使用Mobilenet和Keras错误转移学习

时间:2019-11-29 20:21:34

标签: python keras mobilenet

我是机器学习的新手,我试图为图像分类器制造一个移动网络,以便对是否患有肿瘤的MRI扫描进行分类,我遵循了 https://towardsdatascience.com/transfer-learning-using-mobilenet-and-keras-c75daf7ff299

这使我有了这段代码,但我不断收到此错误

C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ pythonw.exe C:/Users/Acer/PycharmProjects/condas/rawr.py 使用TensorFlow后端 2019-11-30 04:43:31.884511:I tensorflow / core / platform / cpu_feature_guard.cc:142]您的CPU支持该TensorFlow二进制文件未编译为使用的指令:AVX2 C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ keras_applications \ mobilenet.py:207:UserWarning:input_shape是未定义或非正方形的,或者rows不是[128、160、192、224]。输入形状的权重(224、224)将作为默认值加载。   warnings.warn('input_shape是未定义的或非正方形的,' 找到属于2类的393张图像。 时代1/10 追溯(最近一次通话):   在第54行的“ C:/Users/Acer/PycharmProjects/condas/rawr.py”文件中     纪元= 10)   包装中的文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ keras \ legacy \ interfaces.py”,行91     return func(* args,** kwargs)   在fit_generator中的文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ keras \ engine \ training.py”,行1732     initial_epoch = initial_epoch)   在fit_generator的第185行中,文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ keras \ engine \ training_generator.py”     generator_output =下一步(output_generator)   在get中的文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ keras \ utils \ data_utils.py”,行625     six.reraise(* sys.exc_info())   重新增加文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ six.py”的第693行     提高价值   获取文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ keras \ utils \ data_utils.py”,行610     输入= future.get(超时= 30)   在获得的文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ multiprocessing \ pool.py”中,行644     提高自我价值   工作器中的文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ multiprocessing \ pool.py”,行119     结果=(真,func(* args,** kwds))   文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ keras \ utils \ data_utils.py”,第406行,位于get_index中     返回_SHARED_SEQUENCES [uid] [i]    getitem 中的文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ keras_preprocessing \ image \ iterator.py”,第65行。     返回self._get_batches_of_transformed_samples(index_array)   文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ keras_preprocessing \ image \ iterator.py”,行230,位于_get_batches_of_transformed_samples中     插值= self.interpolation)   文件“ C:\ Users \ Acer \ Anaconda3 \ envs \ condas \ lib \ site-packages \ keras_preprocessing \ image \ utils.py”,第108行,在load_img中     引发ImportError('无法导入PIL.Image。' ImportError:无法导入PIL.Image。使用load_img需要PIL。

以退出代码1完成的过程

import keras
from keras import backend as K
from keras.layers.core import Dense, Activation
from keras.optimizers import Adam
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.models import Model
from keras.applications import imagenet_utils
from keras.layers import Dense,GlobalAveragePooling2D
from keras.applications import MobileNet
from keras.applications.mobilenet import preprocess_input
import numpy as np
from IPython.display import Image
from keras.optimizers import Adam
import pickle

mobile = keras.applications.mobilenet.MobileNet()
def prepare_image(file):
    img_path = 'C:/Users/Acer/imagerec/EDA'
    img = image.load_img(img_path + file, target_size=(224, 224))
    img_array = image.img_to_array(img)
    img_array_expanded_dims = np.expand_dims(img_array, axis=0)
    return keras.applications.mobilenet.preprocess_input(img_array_expanded_dims)

base_model=MobileNet(weights='imagenet',include_top=False) #imports the mobilenet model and discards the last 1000 neuron layer.

x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.
x=Dense(1024,activation='relu')(x) #dense layer 2
x=Dense(512,activation='relu')(x) #dense layer 3
preds=Dense(2,activation='softmax')(x) #final layer with softmax activation

model=Model(inputs=base_model.input,outputs=preds)

train_datagen=ImageDataGenerator(preprocessing_function=preprocess_input) #included in our dependencies

train_generator=train_datagen.flow_from_directory('C:/Users/Acer/imagerec/EDA',
                                                 target_size=(224,224),
                                                 color_mode='grayscale',
                                                 batch_size=15,
                                                 class_mode='categorical',
                                                 shuffle=True)

model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])
# Adam optimizer
# loss function will be categorical cross entropy
# evaluation metric will be accuracy

step_size_train=train_generator.n//train_generator.batch_size
model.fit_generator(generator=train_generator,
                   steps_per_epoch=step_size_train,
                   epochs=10)

0 个答案:

没有答案