Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
失败的前提条件:Python解释器状态未初始化。该过程可能被终止_Python_Tensorflow_Machine Learning_Keras_Deep Learning - Fatal编程技术网

失败的前提条件:Python解释器状态未初始化。该过程可能被终止

失败的前提条件:Python解释器状态未初始化。该过程可能被终止,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,我试着训练我的形象。此数据的大小为50000张图像 我的图像属性是: 如果我要改变我的图像属性,我该怎么做 这是我的第一个图像分类器工作,所以可能会有很多错误。请理解 你能帮我改进代码吗 这是我的代码 from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D,MaxPooling2D,Activation,Dropout,Flatten,Dense from tensorf

我试着训练我的形象。此数据的大小为50000张图像

我的图像属性是:

如果我要改变我的图像属性,我该怎么做

这是我的第一个图像分类器工作,所以可能会有很多错误。请理解

你能帮我改进代码吗

这是我的代码

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,MaxPooling2D,Activation,Dropout,Flatten,Dense
from tensorflow.keras.preprocessing.image import ImageDataGenerator,img_to_array,load_img
import matplotlib.pyplot as plt
from glob import glob
from PIL import Image
import os


import numpy as np

train_path="Düzgün2/"

images = [train_path for train_path in os.listdir() if train_path.endswith(('jpeg', 'png', 'jpg'))]
for x in images:
    img = Image.open(x)
    img.thumbnail((600,600))
    img.save("resized_"+x, optimize=True, quality=40)
test_path="Test/"
data=load_img(train_path + "Basler_acA1440-220um__40052667__20201123_114618747_7932_result.jpg")
x=np.asarray(data)

plt.imshow(data)
plt.axis("off")
plt.show()


print(x.shape)
className=glob(train_path + "/*")
print(className)
numberOfClass=len(className)
print("NumberOfClass:",numberOfClass)

#CNN model

model=Sequential()
model.add(Conv2D(32,(3,3),input_shape=(224,224,3)))#xshape burada gelen resimin pixseli 3 ise rgb yi tesmil ediyor so that senin çivi resimlerine göre ayarla
model.add(Activation("relu"))
model.add(MaxPooling2D())
model.add(Conv2D(32,(3,3)))#xshape burada gelen resimin pixseli 3 ise rgb yi tesmil ediyor so that senin çivi resimlerine göre ayarla"""
model.add(Activation("relu"))
model.add(MaxPooling2D())

model.add(Flatten())
model.add(Dense(1024))#1024 nörondan oluşuyor
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(numberOfClass))#output
model.add(Activation("softmax"))

model.compile(loss="categorical_crossentropy",optimizer="rmsprop",metrics=["accuracy"])

batch_size=32# her iterasyonda 32 resmi kullan

#data generator

train_datagen=ImageDataGenerator(rescale=1./255,shear_range=0.3,
                   horizontal_flip=True,zoom_range=0.3)#rgb 0-255 0-1 ile bir arası değer alıyoruz,shear sağa sola yatırma
test_datagen=(ImageDataGenerator(rescale=1./255))

train_generator=train_datagen.flow_from_directory(train_path,
                                                 target_size=x.shape[:2],batch_size=batch_size,color_mode="rgb",
                                                 class_mode="categorical")
test_generator=test_datagen.flow_from_directory(test_path,
                                                 target_size=x.shape[:2],batch_size=batch_size,color_mode="rgb",
                                                 class_mode="categorical")

model.fit_generator(generator=train_generator,steps_per_epoch=1600 // batch_size,epochs=100,validation_data=test_generator,
                        validation_steps=800 // batch_size)
   
这是我的输出:

NumberOfClass: 11376
2020-12-15 14:10:26.449262: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-12-15 14:10:27.159243: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce GTX 1660 Ti computeCapability: 7.5
coreClock: 1.59GHz coreCount: 24 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 268.26GiB/s
2020-12-15 14:10:27.159530: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-12-15 14:10:27.221768: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-12-15 14:10:27.269324: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-12-15 14:10:27.275602: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-12-15 14:10:27.301737: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-12-15 14:10:27.314461: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-12-15 14:10:27.407152: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-12-15 14:10:27.407410: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2020-12-15 14:10:27.407830: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-12-15 14:10:27.416924: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x193e2fdee00 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-12-15 14:10:27.417440: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-12-15 14:10:27.417880: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce GTX 1660 Ti computeCapability: 7.5
coreClock: 1.59GHz coreCount: 24 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 268.26GiB/s
2020-12-15 14:10:27.418242: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-12-15 14:10:27.418413: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-12-15 14:10:27.418586: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-12-15 14:10:27.418754: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-12-15 14:10:27.418922: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-12-15 14:10:27.419095: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-12-15 14:10:27.419268: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-12-15 14:10:27.419477: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2020-12-15 14:10:28.461954: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-12-15 14:10:28.462106: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]      0 
2020-12-15 14:10:28.462194: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0:   N 
2020-12-15 14:10:28.462442: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4750 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1660 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5)
2020-12-15 14:10:28.465422: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1942c7ee0b0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-12-15 14:10:28.465660: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): GeForce GTX 1660 Ti, Compute Capability 7.5
Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.
WARNING:tensorflow:From C:/Users/tatu/PycharmProjects/pythonProject4/main.py:70: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
Please use Model.fit, which supports generators.
Epoch 1/100
2020-12-15 14:10:30.182839: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
Traceback (most recent call last):
  File "C:/Users/tatu/PycharmProjects/pythonProject4/main.py", line 70, in <module>
    validation_steps=800 // batch_size)
  File "C:\Users\tatu\miniconda3\envs\tensorfloww\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "C:\Users\tatu\miniconda3\envs\tensorfloww\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1479, in fit_generator
    initial_epoch=initial_epoch)
  File "C:\Users\tatu\miniconda3\envs\tensorfloww\lib\site-packages\tensorflow\python\keras\engine\training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:\Users\tatu\miniconda3\envs\tensorfloww\lib\site-packages\tensorflow\python\keras\engine\training.py", line 848, in fit
    tmp_logs = train_function(iterator)
  File "C:\Users\tatu\miniconda3\envs\tensorfloww\lib\site-packages\tensorflow\python\eager\def_function.py", line 580, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\tatu\miniconda3\envs\tensorfloww\lib\site-packages\tensorflow\python\eager\def_function.py", line 644, in _call
    return self._stateless_fn(*args, **kwds)
  File "C:\Users\tatu\miniconda3\envs\tensorfloww\lib\site-packages\tensorflow\python\eager\function.py", line 2420, in __call__
    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
  File "C:\Users\tatu\miniconda3\envs\tensorfloww\lib\site-packages\tensorflow\python\eager\function.py", line 1665, in _filtered_call
    self.captured_inputs)
  File "C:\Users\tatu\miniconda3\envs\tensorfloww\lib\site-packages\tensorflow\python\eager\function.py", line 1746, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "C:\Users\tatu\miniconda3\envs\tensorfloww\lib\site-packages\tensorflow\python\eager\function.py", line 598, in call
    ctx=ctx)
  File "C:\Users\tatu\miniconda3\envs\tensorfloww\lib\site-packages\tensorflow\python\eager\execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Reduction axis -1 is empty in shape [0,0]
     [[node ArgMax (defined at /Users/tatu/PycharmProjects/pythonProject4/main.py:70) ]] [Op:__inference_train_function_921]

Function call stack:
train_function

2020-12-15 14:10:30.637723: W tensorflow/core/kernels/data/generator_dataset_op.cc:103] Error occurred when finalizing GeneratorDataset iterator: Failed precondition: Python interpreter state is not initialized. The process may be terminated.
     [[{{node PyFunc}}]]

Process finished with exit code 1

[编辑] 如前所述,输入模型需要与图像形状匹配

如果您正在使用此选项:

 model.add(Conv2D(32,(3,3),input_shape=(224,224,3)))
您的输入图像尺寸应该是(2242243),如您所示,输入是1440x1080,但您正在将其转换为600x600,对吗?选择一种分辨率大小并坚持使用。但是如果使用的是预训练模型,则需要使用模型的分辨率


不能更改已训练或已构建模型的输入图层形状。这就是他抱怨的。预期的输入有4个维度,您将通过3个维度

model.build(input_shape=(150, 150, 3)) # what you did
model.build(input_shape=(w, x, y, z)) # what you should do
首先,检查你数据的维度,它们是RGB,RGBA,我假设你使用的是CNN,你处理的是图像(可能不是真的)。如果数据形状与模型不兼容,如果要使用相同的模型,可能需要转换数据。另一种方法是查找或创建另一个使用数据形状作为输入的模型


正如David Waterworth所指出的,我在这里假设,在您的例子中,第一个维度w与所使用的bash大小相关,并且(x,y,z)与数据本身相关。

只需在程序之前,即导入依赖项之后添加这个维度

GPU=tf.config.experimental.list\u物理\u设备('GPU'))
tf.config.experimental.set\u memory\u growth(GPU[0],True)

我很确定这不是您的完整代码。没有导入?它不需要批处理吗?这条信息对我来说意味着它期望的是4D而不是3D张量?所以也许(批次,x,y,z)-我在猜。我编辑了我的问题。你能再检查一下我的问题吗?非常感谢。@TTA做到了,=)谢谢,我理解,但我还有一个错误。我正在重新编辑这个问题。你能再帮我一次吗?:)很抱歉,stackoverflow不是这样工作的。你应该发布一个问题并继续前进,如果你遇到了另一个问题,找出别人以前是否有过,如果你没有再次发布。这将使社区保持更好的形式。
model.build(input_shape=(150, 150, 3)) # what you did
model.build(input_shape=(w, x, y, z)) # what you should do