700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python狗图像识别_python+keras:识别狗的品种 准确率超过80%!

python狗图像识别_python+keras:识别狗的品种 准确率超过80%!

时间:2024-05-06 17:33:07

相关推荐

python狗图像识别_python+keras:识别狗的品种 准确率超过80%!

model.fit(train_generator, epochs=5, validation_data=valid_generator, callbacks=[checkpointer])我们使用一个ModelCheckpoint的回调来保存验证分数较高的模型。通过测试模型,我们得到的准确率只有1%左右使用迁移学习现在,我们使用迁移学习来实现更高的准确率。首先我们下载ResNet-50,可以通过运行下面的代码来提取相应的训练集、测试和验证集:bottleneck_features = np.load('Data/bottleneck_features/DogResnet50Data.npz')train_Resnet50 = bottleneck_features['train']valid_Resnet50 = bottleneck_features['valid']test_Resnet50 = bottleneck_features['test']我们现在再次定义模型,并对提取的特征使用GlobalAveragePooling2D,它将一组特征平均为一个值。最后,如果验证损失在两个连续的epoch内没有增加,我们使用额外的回调来降低学习率;如果验证损失在连续的5个epoch内没有增加,可以提前停止训练。Resnet50_model = tf.keras.models.Sequential()Resnet50_model.add(tf.keras.layers.GlobalAveragePooling2D(input_shape=train_Resnet50.shape[1:]))Resnet50_model.add(tf.keras.layers.Dense(1024, activation='relu'))Resnet50_model.add(tf.keras.layers.Dense(133, activation='softmax'))

Resnet50_model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath='saved_models/weights_best_Resnet50.hdf5',verbose=1, save_best_only=True)early_stopping = tf.keras.callbacks.EarlyStopping(patience=5, monitor='val_loss')

reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(patience=2, monitor='val_loss')Resnet50_model.fit(train_Resnet50, train_targets,validation_data=(valid_Resnet50, valid_targets),epochs=50, batch_size=20, callbacks=[checkpointer, early_stopping, reduce_lr], verbose=1)### 训练模型最后在测试集上的准确率为82.65%,这与我们白手起家训练的模型相比,是一个巨大的进步。构建web应用程序对于web应用程序,我们首先编写了一个helper函数,该函数接受图像路径并返回品种。label_to_cat字典将每个数字标签映射到它的狗品种。def predict_breed(img_path): '''预测给定图像的品种''' # 提取特征 bottleneck_feature = extract_Resnet50(path_to_tensor(img_path)) bottleneck_feature = tf.keras.models.Sequential([ tf.keras.layers.GlobalAveragePooling2D(input_shape=bottleneck_feature.shape[1:]) ]).predict(bottleneck_feature).reshape(1, 1, 1, 2048) # 获得预测向量 predicted_vector = Resnet50_model.predict(bottleneck_feature) # 模型预测的犬种 return label_to_cat[np.argmax(predicted_vector)]对于web应用程序,我们将使用flaskweb框架来帮助我们用最少的代码创建web应用程序。我们定义一个接受图像的路由,并用狗的品种呈现一个输出模板@app.route('/upload', methods=['POST','GET'])def upload_file(): if request.method == 'GET': return render_template('index.html') else: file = request.files['image'] full_name = os.path.join(UPLOAD_FOLDER, file.filename) file.save(full_name) dog_breed = dog_breed_classifier(full_name) return render_template('predict.html', image_file_name = file.filename, label = dog_breed)predict.html是分别显示图像及其犬种的模板。结论祝贺你!你已经成功地实现了一个狗品种分类器,并且可以准确地分辨出狗的品种。让我们总结一下我们在这里学到的知识:我们对数据集进行了分析和预处理。机器学习算法需要单独的训练集、测试集和验证集来进行置信预测。我们从零开始使用CNN,由于未能提取特征,所以表现不佳。然后我们使用了迁移学习,准确度大大提高最后,我们构建了一个Flask web应用程序来实现我们的项目封装我们确实学到了很多东西,但你还可以尝试很多其他的事情。你可以在heroku上部署web应用程序,也可以尝试使用不同的层(如Dropout层)来提高准确性。参考链接:https://towardsdatascience.com/dont-know-the-breed-of-your-dog-ml-can-help-6558eb5f7f05

☆ END ☆

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。