033. conver colored image to vector,use cnn model # @ # I want to convert image to vector, # but with keeping color data from PIL import Image import numpy as np with open("tower.jpg","rb") as file: opened_file=Image.open(file) converted_image_to_rgb=opened_file.convert("RGB") # You reduce image size by reshape resized_image=converted_image_to_rgb.resize((64,64)) converted_image_to_nparray=np.array(resized_image) # @ from PIL import Image import os,glob import numpy as np from sklearn.model_selection import train_test_split # This is folder I want to load folder_you_want_to_open="./image/101_ObjectCategories" # These are categories_list which I want to use categories_list=["chair","camera","butterfly","elephant","flamingo"] # You define number_of_classes by len(categories_list) number_of_classes=len(categories_list) # You define size of image as 64*64 width_of_image=64 height_of_image=64 # Since you will use RGB information, # you should multiply 3, # to calculate entire number of element of image vector number_of_element_of_converted_vector_from_image=width_of_image*height_of_image*3 # You will load image data X_feature_of_data_list=[] Y_label_of_data_list=[] for index,category in enumerate(categories_list): # You create list for label_list data label_list=[0 for index in range(number_of_classes)] label_list[index]=1 # You define image folder image_directory=folder_you_want_to_open+"/"+category # glob can load every file in specified folder loaded_files_from_specified_folder=glob.glob(image_directory+"/*.jpg") for index,one_file in enumerate(loaded_files_from_specified_folder): opened_file=Image.open(one_file) converted_file_to_rgb=opened_file.convert("RGB") resized_file=converted_file_to_rgb.resize((width_of_image,height_of_image)) filed_represented_in_nparray=np.asarray(resized_file) X_feature_of_data_list.append(filed_represented_in_nparray) Y_label_of_data_list.append(label_list) if index%10==0: print(index,"\n",data) X_feature_of_data_nparray=np.array(X_feature_of_data_list) Y_label_of_data_nparray=np.array(Y_label_of_data_list) # I separate train dataset and test dataset x_train_data,x_test_data,y_train_data,y_test_data\ =train_test_split(X_feature_of_data_list,Y_label_of_data_list) xy_data_set=(x_train_data,x_test_data,y_train_data,y_test_data) np.save("./image/5obj.npy",xy_data_set) print("ok,",len(Y_label_of_data_list)) # @ Classifying image by cnn from keras.models import Sequential from keras.layers import Convolution2D,MaxPooling2D from keras.layers import Activation,Dropout,Flatten,Dense import h5py from PIL import Image import numpy as np import os # You define categories categories_list=["chair","camera","butterfly","elephant","flamingo"] # You define number_of_classes by len(categories_list) number_of_classes=len(categories_list) # Since you created image data, # whose size is 64*64, # you will disgnate 64,64 width_of_image=64 height_of_image=64 # You load 5obj.npy file, # which you created from above x_train_data,x_test_data,y_train_data,y_test_data=np.load("./image/5obj.npy") # You normalize data, # because input data should have value ranged from 0 to 1 normalized_x_train_data=x_train_data.astype("float")/256 normalized_x_test_data =x_test_data.astype("float") /256 print("normalized_x_train_data.shape:",normalized_x_train_data.shape) # You implement cnn model # Following scheme is ordinal one, # which can be applied to in almost cases sequential_object=Sequential() # You perform filter sliding sequential_object.add(Convolution2D(32,3,3, border_mode='same', input_shape=x_train_data.shape[1:])) # You perform relu funtion sequential_object.add(Activation('relu')) # You perform pooling sequential_object.add(MaxPooling2D(pool_size=(2,2))) # You perform dropout sequential_object.add(Dropout(0.25)) # You perform 2nd convolution layer sequential_object.add(Convolution2D(64,3,3,border_mode='same')) sequential_object.add(Activation('relu')) sequential_object.add(Convolution2D(64,3,3)) sequential_object.add(MaxPooling2D(pool_size=(2,2))) sequential_object.add(Dropout(0.25)) sequential_object.add(Flatten()) sequential_object.add(Dense(512)) sequential_object.add(Activation('relu')) sequential_object.add(Dropout(0.5)) sequential_object.add(Dense(number_of_classes)) sequential_object.add(Activation('softmax')) # You actually run built model sequential_object.compile(\ loss='binary_crossentropy'\ ,optimizer='rmsprop'\ ,metrics=['accuracy']) # You train model hdf5_file="./image/5obj-sequential_object.hdf5" if os.path.exists(hdf5_file): sequential_object.load_weights(hdf5_file) else: sequential_object.fit(x_train_data,y_train_data,batch_size=32,nb_epoch=50) sequential_object.save_weights(hdf5_file) # You evaluate model prediction_values=sequential_object.predict(x_test_data) for index,prediction_value in enumerate(prediction_values): prediction_value_after_argmax=prediction_value.argmax() label_value_after_argmax=y_test_data[index].argmax() one_x_feature_test_data=x_test_data[index] if label_value_after_argmax==prediction_value_after_argmax: continue print("[NG]",categories_list[prediction_value_after_argmax],"!=",categories_list[label_value_after_argmax]) print(prediction_value) filename_string\ ="image/error/"+str(index)+"-"+categories_list[prediction_value_after_argmax]\ +"-ne-"+categories_list[label_value_after_argmax]+".png" one_x_feature_test_data*=256 opened_file=Image.fromarray(np.uint8(one_x_feature_test_data)) opened_file.save(filename_string) score=sequential_object.evaluate(x_test_data,y_test_data) print('loss=',score[0]) print('accuracy=',score[1]) # @ # People who create new models like cnn, rnn are not ordinal developers, # but researchers in constitute # Researchers should know every aspect of model in detail # However, data scientists and ordinal developers, # who apply these models would be better to focus on applying models, # to make benefits rather than creating new models # To apply model, most recommended way is to read related thesis papers, # which are written by researchers