├── .gitignore ├── README.md ├── bottleneck.py ├── fine_tune.py ├── result_picture ├── Training loss and validation loss.jpg ├── bottleneck Training accuracy and loss.jpg ├── bottleneck Training loss and validation loss.jpg ├── bottleneck validation accuracy and loss.jpg ├── tuned Training acc and loss.jpg └── tuned validation acc and loss.jpg ├── test.py ├── train.py └── 基于Keras的图片分类.pptx /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Machine_learn_Image 2 | 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类 3 | -------------------------------------------------------------------------------- /bottleneck.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Apr 15 14:21:13 2018 4 | 5 | @author: Beobachter 6 | """ 7 | 8 | 9 | #导入预训练权重和网络框架 10 | from keras.applications.vgg16 import VGG16 11 | #WEIGHTS_PATH = 'E:\TensorFlowfile\Kerasmodel\vgg16_weights_tf_dim_ordering_tf_kernels.h5' 12 | #WEIGHTS_PATH_NO_TOP = 'E:\TensorFlowfile\Kerasmodel\vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5' 13 | model = VGG16(weights='imagenet',include_top=False) 14 | 15 | #提取bottleneck特征 16 | 17 | #载入图片 18 | from keras.preprocessing.image import ImageDataGenerator 19 | import numpy as np 20 | 21 | def bottleneck(): 22 | 23 | datagen = ImageDataGenerator(rescale = 1./255)#将每个像素缩放至[0,1] 24 | 25 | #训练集图像生成器 26 | train_generator = datagen.flow_from_directory( 27 | 'E:/TensorFlowfile/imageclassification/data/train', 28 | target_size = (250,250), 29 | batch_size = 20, 30 | class_mode = None, 31 | shuffle = False 32 | ) 33 | print('train begin') 34 | 35 | #验证集图像生成器 36 | validation_generator = datagen.flow_from_directory( 37 | 'E:/TensorFlowfile/imageclassification/data/validation', 38 | target_size = (250,250), 39 | batch_size = 20, 40 | class_mode = None, 41 | shuffle = False 42 | ) 43 | 44 | #灌入权重 45 | #model.load_weights(WEIGHTS_PATH_NO_TOP) 46 | 47 | #bottleneck feature 48 | bottleneck_feature_train = model.predict_generator(train_generator,100) 49 | np.save(open('bottleneck_feature_train.npy', 'wb+'), bottleneck_feature_train) 50 | bottleneck_feature_validation = model.predict_generator(validation_generator, 40) 51 | np.save(open('bottleneck_feature_validation.npy', 'wb+'), bottleneck_feature_validation) 52 | print('train done') 53 | 54 | if __name__ == '__main__': 55 | bottleneck() 56 | -------------------------------------------------------------------------------- /fine_tune.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from keras.applications.vgg16 import VGG16 4 | from keras.layers import Activation, Dropout, Flatten,Dense, Input, Reshape 5 | from keras import optimizers 6 | from keras.preprocessing.image import ImageDataGenerator 7 | from keras import regularizers 8 | from keras.models import Model 9 | import matplotlib.pyplot as plt 10 | 11 | 12 | def fine_tune(): 13 | 14 | model = VGG16(weights='imagenet',include_top=False, input_shape=(224, 224, 3)) 15 | base_model = Model(input = model.input, output = model.get_layer("block5_pool").output) 16 | 17 | for layer in model.layers[0:14]: #冻结前14层 18 | layer.trainable = False 19 | 20 | 21 | base_out = base_model.output 22 | base_out = Reshape((25088,))(base_out) #将卷积层输出reshape成一维向量 23 | top_model = Dense(256, activation='relu')(base_out) 24 | top_model = Dropout(0.5)(top_model) 25 | top_preds = Dense(4, activation='softmax',kernel_regularizer = regularizers.l2(0.01), 26 | activity_regularizer=regularizers.l1(0.001))(top_model) 27 | my_model = Model(input = base_model.input, output=top_preds) 28 | 29 | 30 | 31 | 32 | my_model.compile(loss='categorical_crossentropy', 33 | optimizer=optimizers.SGD(lr=1e-4, momentum=0.9), 34 | metrics=['accuracy']) 35 | 36 | train_datagen = ImageDataGenerator( 37 | rescale=1./255, 38 | shear_range=0.2, #剪切强度 39 | zoom_range=0.2, #随机缩放 40 | horizontal_flip=True) #随机水平翻转 41 | 42 | test_datagen = ImageDataGenerator(rescale=1./255) 43 | 44 | train_generator = train_datagen.flow_from_directory( 45 | 'E:/TensorFlowfile/imageclassification/data/train', 46 | target_size=(224, 224), 47 | batch_size=20, 48 | class_mode='categorical') 49 | 50 | validation_generator = test_datagen.flow_from_directory( 51 | 'E:/TensorFlowfile/imageclassification/data/validation', 52 | target_size=(224, 224), 53 | batch_size=20, 54 | class_mode='categorical') 55 | 56 | 57 | hist = my_model.fit_generator( 58 | train_generator, 59 | steps_per_epoch=100, 60 | epochs=50, 61 | validation_data=validation_generator 62 | ) 63 | score = my_model.evaluate_generator(validation_generator, 80) 64 | print('test acc:', score[1]) 65 | my_model.save_weights('tuned_weights.h5') 66 | my_model.save('tuned.h5') 67 | 68 | 69 | acc = hist.history['acc'] 70 | val_acc = hist.history['val_acc'] 71 | loss = hist.history['loss'] 72 | val_loss = hist.history['val_loss'] 73 | 74 | epochs = range(len(acc)) 75 | 76 | plt.plot(epochs, acc, 'bo', label='Training acc') 77 | plt.plot(epochs, loss, 'b', label='Training loss') 78 | plt.title('Training accuracy and loss') 79 | plt.legend() 80 | plt.savefig('tuned Training acc and loss.jpg') 81 | plt.figure() 82 | 83 | plt.plot(epochs, loss, 'r', label='Training loss') 84 | plt.plot(epochs, val_loss, 'b', label='Validation loss') 85 | plt.title('loss') 86 | plt.legend() 87 | plt.savefig('Training loss and validation loss.jpg') 88 | plt.figure() 89 | 90 | plt.plot(epochs, val_acc, 'bo', label='Validation acc') 91 | plt.plot(epochs, val_loss, 'b', label='Validation loss') 92 | plt.title('validation accuracy and loss') 93 | plt.legend() 94 | plt.savefig('tuned validation acc and loss.jpg') 95 | 96 | plt.show() 97 | 98 | 99 | if __name__ == '__main__': 100 | fine_tune() -------------------------------------------------------------------------------- /result_picture/Training loss and validation loss.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/provencesl/Machine_learn_Image/3a5e70b3e297180280e395505051c6a55008e10c/result_picture/Training loss and validation loss.jpg -------------------------------------------------------------------------------- /result_picture/bottleneck Training accuracy and loss.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/provencesl/Machine_learn_Image/3a5e70b3e297180280e395505051c6a55008e10c/result_picture/bottleneck Training accuracy and loss.jpg -------------------------------------------------------------------------------- /result_picture/bottleneck Training loss and validation loss.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/provencesl/Machine_learn_Image/3a5e70b3e297180280e395505051c6a55008e10c/result_picture/bottleneck Training loss and validation loss.jpg -------------------------------------------------------------------------------- /result_picture/bottleneck validation accuracy and loss.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/provencesl/Machine_learn_Image/3a5e70b3e297180280e395505051c6a55008e10c/result_picture/bottleneck validation accuracy and loss.jpg -------------------------------------------------------------------------------- /result_picture/tuned Training acc and loss.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/provencesl/Machine_learn_Image/3a5e70b3e297180280e395505051c6a55008e10c/result_picture/tuned Training acc and loss.jpg -------------------------------------------------------------------------------- /result_picture/tuned validation acc and loss.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/provencesl/Machine_learn_Image/3a5e70b3e297180280e395505051c6a55008e10c/result_picture/tuned validation acc and loss.jpg -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Apr 17 09:20:05 2018 4 | 5 | @author: Beobachter 6 | """ 7 | 8 | from keras.models import load_model 9 | import numpy as np 10 | import os 11 | import matplotlib.pyplot as plt 12 | import matplotlib.image as pimage 13 | from PIL import Image 14 | 15 | 16 | model = load_model('tuned.h5') 17 | #model.summary() 18 | 19 | 20 | 21 | 22 | def test_one_image(test_img): 23 | image = test_img.resize([224, 224]) 24 | image = np.array(image) 25 | im_array = image[np.newaxis, :] 26 | pre = model.predict(im_array) 27 | #print(pre) 28 | matt = np.argmax(pre) 29 | #print(matt) 30 | 31 | if matt == 0: 32 | print('This is a cantaloupe') 33 | if matt == 1: 34 | print('This is a cat') 35 | if matt == 2: 36 | print('This is a dog') 37 | if matt == 3: 38 | print('This is a watermelon') 39 | 40 | test_dir = ['cantaloupe', 'cat', 'dog', 'watermelon'] 41 | 42 | for i in range(len(test_dir)): 43 | img_dir = 'E:/TensorFlowfile/imageclassification/data/test/' + test_dir[i] + '/' 44 | pic = os.listdir(img_dir) 45 | n = len(pic) 46 | ind = np.random.randint(0, n) 47 | img = img_dir + pic[ind] 48 | image = pimage.imread(img) #float(32) 49 | plt.imshow(image) 50 | plt.show() 51 | image = Image.fromarray(np.uint8(image*255)) #uinit8 52 | test_one_image(image) 53 | -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Apr 15 16:34:45 2018 4 | 5 | @author: Beobachter 6 | """ 7 | from keras.models import Sequential 8 | #from keras.layers import Conv2D, MaxPool2D 9 | from keras.layers import Activation, Dropout, Flatten,Dense 10 | import numpy as np 11 | from keras import regularizers 12 | import matplotlib.pyplot as plt 13 | import keras 14 | 15 | def train_fc(): 16 | #导入bottleneck feature 17 | train_data = np.load(open('bottleneck_feature_train.npy', 'rb')) 18 | train_labels = np.array([0] * 500 + [1] * 500 + [2] * 500 + [3] * 500) #打标签 19 | validation_data = np.load(open('bottleneck_feature_validation.npy', 'rb')) 20 | validation_labels = np.array([0] * 200 + [1] * 200 + [2] * 200 + [3] * 200) 21 | 22 | train_labels = keras.utils.to_categorical(train_labels, 4) #将类别向量映射为二值类别矩阵 23 | validation_labels = keras.utils.to_categorical(validation_labels, 4) 24 | 25 | #构建网络 26 | model = Sequential() 27 | model.add(Flatten(input_shape = (7,7,512))) 28 | model.add(Dense(256, activation='relu')) 29 | model.add(Dropout(0.5)) 30 | model.add(Dense(4, activation='softmax', kernel_regularizer = regularizers.l2(0.01), 31 | activity_regularizer=regularizers.l1(0.001))) 32 | model.compile(optimizer='rmsprop', #编译模型 33 | loss = 'categorical_crossentropy', 34 | metrics=['accuracy']) 35 | #训练模型 36 | hist = model.fit(train_data, train_labels, nb_epoch = 50, batch_size=32,validation_data = (validation_data, validation_labels)) 37 | #评估模型 38 | score = model.evaluate(validation_data, validation_labels) 39 | #print('test score:', score[0]) 40 | print('test acc:', score[1]) 41 | 42 | acc = hist.history['acc'] 43 | val_acc = hist.history['val_acc'] 44 | loss = hist.history['loss'] 45 | val_loss = hist.history['val_loss'] 46 | 47 | epochs = range(len(acc)) 48 | 49 | plt.plot(epochs, acc, 'bo', label='Training acc') 50 | plt.plot(epochs, loss, 'b', label='Training loss') 51 | plt.title('bottleneck Training accuracy and loss') 52 | plt.legend() 53 | plt.savefig('bottleneck Training accuracy and loss.jpg') 54 | plt.figure() 55 | 56 | plt.plot(epochs, loss, 'r', label='Training loss') 57 | plt.plot(epochs, val_loss, 'b', label='Validation loss') 58 | plt.title('bottleneck loss') 59 | plt.legend() 60 | plt.savefig('bottleneck Training loss and validation loss.jpg') 61 | plt.figure() 62 | 63 | plt.plot(epochs, val_acc, 'bo', label='Validation acc') 64 | plt.plot(epochs, val_loss, 'b', label='Validation loss') 65 | plt.title('bottleneck validation accuracy and loss') 66 | plt.legend() 67 | plt.savefig('bottleneck validation accuracy and loss.jpg') 68 | 69 | plt.show() 70 | model.save_weights('bottleneck_fc_model.h5') 71 | model.save('imagevgg.h5') 72 | 73 | if __name__ == '__main__': 74 | train_fc() -------------------------------------------------------------------------------- /基于Keras的图片分类.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/provencesl/Machine_learn_Image/3a5e70b3e297180280e395505051c6a55008e10c/基于Keras的图片分类.pptx --------------------------------------------------------------------------------