asataniAIR / Image_DL_Tutorial

画像に対するDeep Learningの応用チュートリアル
MIT License
5 stars 3 forks source link

Getting error "Could not import PIL.Image. The use of `array_to_img` requires PIL." #5

Open rahul171988 opened 6 years ago

rahul171988 commented 6 years ago

Hi Everyone,

I am a beginner to machine learning , so I was trying to create a model for recognizing images refrenced from Kersa blog. I have installed Anaconda 3 on windows 10 and all the packages like tensor flow, keras, scipy, numpy, panda using conda command but when I run a code taken from keras blog I got the error. Below is my code. Can some one help me to kick of this great concept ML?. Thanks in advance.

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D from keras.layers import Activation, Dropout, Flatten, Dense

datagen = ImageDataGenerator( rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') img = load_img('E:/ML_R&D/training_set/cats/cat.3919.jpg') # this is a PIL image x = img_to_array(img) # this is a Numpy array with shape (300, 300, 3) x = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, 300, 300, 3) x.shape

the .flow() command below generates batches of randomly transformed images

and saves the results to the preview/ directory

i = 0 for batch in datagen.flow(x, batch_size=1, save_to_dir='preview', save_prefix='el', save_format='jpeg'): i += 1 if i > 20: break # otherwise the generator would loop indefinitely

MODEL

model = Sequential() model.add(Conv2D(32, (3, 3), input_shape=(300, 300, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2)))

the model so far outputs 3D feature maps (height, width, features)

model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors model.add(Dense(64)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid'))

COMPILE

model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) batch_size = 16

this is the augmentation configuration we will use for training

train_datagen = ImageDataGenerator( rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)

this is the augmentation configuration we will use for testing:

only rescaling

test_datagen = ImageDataGenerator(rescale=1./255)

this is a generator that will read pictures found in subfolers of 'data/train', and indefinitely generate

batches of augmented image data

train_generator = train_datagen.flow_from_directory( 'E:\ML_R&D\training_set\cats', # this is the target directory target_size=(300, 300), # all images will be resized to 300x300 batch_size=batch_size, class_mode='binary') # since we use binary_crossentropy loss, we need binary labels

this is a similar generator, for validation data

validation_generator = test_datagen.flow_from_directory( 'validation', target_size=(300, 300), batch_size=batch_size, class_mode='binary')

TRAINING

model.fit_generator( train_generator, steps_per_epoch=400 // batch_size, epochs=50, validation_data=validation_generator, validation_steps=160 // batch_size)

model.save_weights('50_epochs.h5') # always save your weights after training or during training