DL之VGG16:基于VGG16(Keras)利用Knifey-Spoony数据集对网络架构进行迁移学习
DL之VGG16:基于VGG16(Keras)利用Knifey-Spoony数据集对网络架构迁移学习数据集Dataset之Knifey-Spoony:Knifey-Spoony数据集的简介、下载、使用方法之详细攻略输出结果
设计思路1、基模型
2、思路导图
核心代码model_VGG16.summary() transfer_layer = model_VGG16.get_layer('block5_pool') print('transfer_layer.output:', transfer_layer.output) conv_model = Model(inputs=model_VGG16.input, outputs=transfer_layer.output)VGG16_TL_model = Sequential() # Start a new Keras Sequential model.VGG16_TL_model.add(conv_model) # Add the convolutional part of the VGG16 model from above.VGG16_TL_model.add(Flatten()) # Flatten the output of the VGG16 model because it is from a convolutional layer.VGG16_TL_model.add(Dense(1024, activation='relu')) # Add a dense (aka. fully-connected) layer. This is for combining features that the VGG16 model has recognized in the image.VGG16_TL_model.add(Dropout(0.5)) # Add a dropout-layer which may prevent overfitting and improve generalization ability to unseen data e.g. the test-set.VGG16_TL_model.add(Dense(num_classes, activation='softmax')) # Add the final layer for the actual classification.print_layer_trainable() conv_model.trainable = Falsefor layer in conv_model.layers: layer.trainable = Falseprint_layer_trainable() loss = 'categorical_crossentropy' metrics = ['categorical_accuracy'] VGG16_TL_model.compile(optimizer=optimizer, loss=loss, metrics=metrics) epochs = 20steps_per_epoch = 100history = VGG16_TL_model.fit_generator(generator=generator_train, epochs=epochs, steps_per_epoch=steps_per_epoch, class_weight=class_weight, validation_data=generator_test, validation_steps=steps_test)plot_training_history(history) VGG16_TL_model_result = VGG16_TL_model.evaluate_generator(generator_test, steps=steps_test)print("Test-set classification accuracy: {0:.2%}".format(VGG16_TL_model_result[1])) 更多输出输出tensorflow的版本: 1.10.0Data has apparently already been downloaded and unpacked.maybe_download_and_extract()函数执行结束!load()函数的data_dir: data/knifey-spoony/Creating dataset from the files in: data/knifey-spoony/- Data loaded from cache-file: data/knifey-spoony/knifey-spoony.pkl执行load()函数结束!get_paths()函数的self.in_dir输出: data/knifey-spoony- Copied training-set to: data/knifey-spoony/train/get_paths()函数的self.in_dir输出: data/knifey-spoony- Copied test-set to: data/knifey-spoony/test/data/knifey-spoony/train/ data/knifey-spoony/test/……383418368/553467096 [===================>..........] - ETA: 2:09383614976/553467096 [===================>..........] - ETA: 2:09383811584/553467096 [===================>..........] - ETA: 2:09383860736/553467096 [===================>..........] - ETA: 2:09383942656/553467096 [===================>..........] - ETA: 2:09……394297344/553467096 [====================>.........] - ETA: 2:00394510336/553467096 [====================>.........] - ETA: 1:59394723328/553467096 [====================>.........] - ETA: 1:59394821632/553467096 [====================>.........] - ETA: 1:59395018240/553467096 [====================>.........] - ETA: 1:59395214848/553467096 [====================>.........] - ETA: 1:59395395072/553467096 [====================>.........] - ETA: 1:59395542528/553467096 [====================>.........] - ETA: 1:58……469909504/553467096 [========================>.....] - ETA: 1:00470040576/553467096 [========================>.....] - ETA: 1:00470122496/553467096 [========================>.....] - ETA: 59s 470351872/553467096 [========================>.....] - ETA: 59s470499328/553467096 [========================>.....] - ETA: 59s470630400/553467096 [========================>.....] - ETA: 59s470712320/553467096 [========================>.....] - ETA: 59s470925312/553467096 [========================>.....] - ETA: 59s471089152/553467096 [========================>.....] - ETA: 59s471220224/553467096 [========================>.....] - ETA: 59s471302144/553467096 [========================>.....] - ETA: 59s471515136/553467096 [========================>.....] - ETA: 58s471678976/553467096 [========================>.....] - ETA: 58s……536248320/553467096 [============================>.] - ETA: 12s536772608/553467096 [============================>.] - ETA: 11s537329664/553467096 [============================>.] - ETA: 11s537378816/553467096 [============================>.] - ETA: 11s537444352/553467096 [============================>.] - ETA: 11s537640960/553467096 [============================>.] - ETA: 11s537755648/553467096 [============================>.] - ETA: 11s537788416/553467096 [============================>.] - ETA: 10s537821184/553467096 [============================>.] - ETA: 10s……551862272/553467096 [============================>.] - ETA: 1s551993344/553467096 [============================>.] - ETA: 1s552042496/553467096 [============================>.] - ETA: 0s552124416/553467096 [============================>.] - ETA: 0s552337408/553467096 [============================>.] - ETA: 0s552501248/553467096 [============================>.] - ETA: 0s552517632/553467096 [============================>.] - ETA: 0s552583168/553467096 [============================>.] - ETA: 0s552697856/553467096 [============================>.] - ETA: 0s552910848/553467096 [============================>.] - ETA: 0s553041920/553467096 [============================>.] - ETA: 0s553123840/553467096 [============================>.] - ETA: 0s553287680/553467096 [============================>.] - ETA: 0s553467904/553467096 [==============================] - 386s 1us/step2019-08-14 11:44:51.782638: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX22019-08-14 11:44:53.212742: W T:\src\github\tensorflow\tensorflow\core\framework\allocator.cc:108] Allocation of 411041792 exceeds 10% of system memory.2019-08-14 11:44:54.302588: W T:\src\github\tensorflow\tensorflow\core\framework\allocator.cc:108] Allocation of 411041792 exceeds 10% of system memory.2019-08-14 11:44:54.310978: W T:\src\github\tensorflow\tensorflow\core\framework\allocator.cc:108] Allocation of 411041792 exceeds 10% of system memory.(224, 224)Found 4170 images belonging to 5 classes.Found 530 images belonging to 5 classes.26.5['forky', 'knifey', 'spoony', 'test', 'train']5class_weight: [1.39839034 1.14876033 0.70701933]['forky', 'knifey', 'spoony', 'test', 'train']Downloading data from https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json 8192/35363 [=====>........................] - ETA: 0s16384/35363 [============>.................] - ETA: 0s40960/35363 [==================================] - 0s 6us/step79.02% : macaw 6.61% : bubble 3.64% : vine_snake 1.90% : pinwheel 1.22% : knot50.31% : shower_curtain17.08% : handkerchief12.75% : mosquito_net 2.87% : window_shade 1.32% : toilet_tissue45.08% : shower_curtain21.84% : mosquito_net11.55% : handkerchief 2.02% : window_shade 0.91% : Windsor_tie26.75% : spoonbill 7.06% : black_stork 7.04% : wooden_spoon 4.21% : limpkin 3.72% : paddle_________________________________________________________________Layer (type) Output Shape Param # =================================================================input_1 (InputLayer) (None, 224, 224, 3) 0 _________________________________________________________________block1_conv1 (Conv2D) (None, 224, 224, 64) 1792 _________________________________________________________________block1_conv2 (Conv2D) (None, 224, 224, 64) 36928 _________________________________________________________________block1_pool (MaxPooling2D) (None, 112, 112, 64) 0 _________________________________________________________________block2_conv1 (Conv2D) (None, 112, 112, 128) 73856 _________________________________________________________________block2_conv2 (Conv2D) (None, 112, 112, 128) 147584 _________________________________________________________________block2_pool (MaxPooling2D) (None, 56, 56, 128) 0 _________________________________________________________________block3_conv1 (Conv2D) (None, 56, 56, 256) 295168 _________________________________________________________________block3_conv2 (Conv2D) (None, 56, 56, 256) 590080 _________________________________________________________________block3_conv3 (Conv2D) (None, 56, 56, 256) 590080 _________________________________________________________________block3_pool (MaxPooling2D) (None, 28, 28, 256) 0 _________________________________________________________________block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160 _________________________________________________________________block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808 _________________________________________________________________block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808 _________________________________________________________________block4_pool (MaxPooling2D) (None, 14, 14, 512) 0 _________________________________________________________________block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808 _________________________________________________________________block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808 _________________________________________________________________block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808 _________________________________________________________________block5_pool (MaxPooling2D) (None, 7, 7, 512) 0 _________________________________________________________________flatten (Flatten) (None, 25088) 0 _________________________________________________________________fc1 (Dense) (None, 4096) 102764544 _________________________________________________________________fc2 (Dense) (None, 4096) 16781312 _________________________________________________________________predictions (Dense) (None, 1000) 4097000 =================================================================Total params: 138,357,544Trainable params: 138,357,544Non-trainable params: 0_________________________________________________________________Tensor("block5_pool/MaxPool:0", shape=(?, 7, 7, 512), dtype=float32)True:input_1True:block1_conv1True:block1_conv2True:block1_poolTrue:block2_conv1True:block2_conv2True:block2_poolTrue:block3_conv1True:block3_conv2True:block3_conv3True:block3_poolTrue:block4_conv1True:block4_conv2True:block4_conv3True:block4_poolTrue:block5_conv1True:block5_conv2True:block5_conv3True:block5_poolFalse:input_1False:block1_conv1False:block1_conv2False:block1_poolFalse:block2_conv1False:block2_conv2False:block2_poolFalse:block3_conv1False:block3_conv2False:block3_conv3False:block3_poolFalse:block4_conv1False:block4_conv2False:block4_conv3False:block4_poolFalse:block5_conv1False:block5_conv2False:block5_conv3False:block5_pool--------------Epoch 1/20 1/100 [..............................] - ETA: 24:24 - loss: 2.0064 - categorical_accuracy: 0.2500……100/100 [==============================] - 4064s 41s/step - loss: 1.1529 - categorical_accuracy: 0.4490 - val_loss: 0.8731 - val_categorical_accuracy: 0.6189…………100/100 [==============================] - 2850s 29s/step - loss: 0.9524 - categorical_accuracy: 0.5480 - val_loss: 0.8089 - val_categorical_accuracy: 0.6377Epoch 3/20 1/100 [..............................] - ETA: 22:19 - loss: 0.6235 - categorical_accuracy: 0.8000…… 99/100 [============================>.] - ETA: 18s - loss: 0.8497 - categorical_accuracy: 0.6056100/100 [==============================] - 2404s 24s/step - loss: 0.8499 - categorical_accuracy: 0.6060 - val_loss: 0.7322 - val_categorical_accuracy: 0.7283………… 99/100 [============================>.] - ETA: 11s - loss: 0.6253 - categorical_accuracy: 0.7389100/100 [==============================] - 1519s 15s/step - loss: 0.6248 - categorical_accuracy: 0.7390 - val_loss: 0.5702 - val_categorical_accuracy: 0.7811Epoch 10/20 1/100 [..............................] - ETA: 21:14 - loss: 0.4481 - categorical_accuracy: 0.8000…… 99/100 [============================>.] - ETA: 12s - loss: 0.6033 - categorical_accuracy: 0.7490100/100 [==============================] - 1570s 16s/step - loss: 0.6045 - categorical_accuracy: 0.7475 - val_loss: 0.5199 - val_categorical_accuracy: 0.8075Epoch 11/20 1/100 [..............................] - ETA: 19:40 - loss: 0.5531 - categorical_accuracy: 0.7500…… 99/100 [============================>.] - ETA: 12s - loss: 0.5403 - categorical_accuracy: 0.7813100/100 [==============================] - 1559s 16s/step - loss: 0.5401 - categorical_accuracy: 0.7810 - val_loss: 0.5147 - val_categorical_accuracy: 0.8132Epoch 15/20 1/100 [..............................] - ETA: 20:10 - loss: 0.5337 - categorical_accuracy: 0.7000 2/100 [..............................] - ETA: 19:46 - loss: 0.4598 - categorical_accuracy: 0.8250…… 99/100 [============================>.] - ETA: 12s - loss: 0.5495 - categorical_accuracy: 0.7601100/100 [==============================] - 1578s 16s/step - loss: 0.5482 - categorical_accuracy: 0.7610 - val_loss: 0.5832 - val_categorical_accuracy: 0.7491Epoch 16/20 1/100 [..............................] - ETA: 20:07 - loss: 0.2315 - categorical_accuracy: 1.0000…… 19/100 [====>.........................] - ETA: 16:29 - loss: 0.5293 - categorical_accuracy: 0.7816