ResNet50图像分类 水果图像分类 确定水果的质量类别,用于检测苹果香蕉番石榴、酸橙、橙子、石榴等水果品质数据集和质量检测分类数据集

ResNet50图像分类 水果图像分类 确定水果的质量类别,用于检测苹果香蕉番石榴、酸橙、橙子、石榴等水果品质数据集和质量检测分类数据集 构建一个用于水果检测和质量分类的深度学习模型。我们将使用YOLOv5You Only Look Once version 5进行目标检测并结合ResNet50进行图像分类以确定水果的质量类别。那么这个怎么处理水果质量数据集(用于水果检测和质量分类的自定义数据集数据集概述图像总数1,968 张图像训练集1,852 张图像验证集116 张图像水果种类苹果、香蕉、番石榴、酸橙、橙子、石榴质量类别该数据集将水果分为不同的成熟阶段捕捉了广泛的成熟条件质量差质量好用例此数据集特别适用于水果品质分类旨在区分水果的质量这在农业技术、质量控制和食品工业中有应用。AI 和计算机视觉项目非常适合训练深度学习模型以检测水果图像中的成熟度。农业研究促进对水果质量和成熟度自动识别的研究这是食品物流和减少浪费的关键因素。数据集详细信息图像尺寸图像的大小主要为 256x256 像素来自不同的真实环境在不同的照明条件和背景例如俯视图、前视图、旋转方向下捕获。注释每张图像都标有边界框坐标用于检测水果便于完成对象检测任务以及成熟度分类。**备注文章代码仅供参考**了解了您的数据集详细信息后我们可以开始构建一个用于水果检测和质量分类的深度学习模型。我们将使用YOLOv5You Only Look Once version 5进行目标检测并结合ResNet50进行图像分类以确定水果的质量类别。以下是详细的步骤环境准备安装必要的库。下载并组织数据集。数据预处理将数据集分为训练集、验证集和测试集。格式化标签文件以便于YOLOv5使用。模型定义与训练使用YOLOv5进行目标检测。使用ResNet50进行质量分类。评估与可视化评估模型性能。可视化结果。环境准备首先我们需要安装YOLOv5和一些其他的依赖项。您可以使用以下命令来设置环境pipinstalltorch torchvision torchaudio pyyaml opencv-python-headless seaborn pandas matplotlib scikit-learngitclone https://github.com/ultralytics/yolov5cdyolov5 pipinstall-rrequirements.txt数据预处理假设您的数据集已经下载并存储在datasets/fruits目录中其中包含train、val和test子目录每个子目录下又有images和labels子目录。labels子目录中的每个.txt文件对应一个图像文件格式如下class_id x_center y_center width height例如如果有一张名为apple_1.jpg的图像对应的标签文件apple_1.txt可能包含以下内容0 0.5 0.5 0.2 0.2这意味着图像中有一个苹果类ID为0其边界框中心点在(0.5, 0.5)宽度和高度均为图像尺寸的20%。接下来我们将编写一个脚本来检查数据集的有效性并生成YOLOv5所需的配置文件。[titleData Preparation Script for Fruit Detection and Quality Classification]importosimportjsonfrompathlibimportPath# Define pathsbase_pathPath(datasets/fruits)train_imagesbase_path/train/imagestrain_labelsbase_path/train/labelsval_imagesbase_path/val/imagesval_labelsbase_path/val/labels# Function to check if corresponding label files existdefcheck_dataset(images_dir,labels_dir):image_filesset([f.stemforfinimages_dir.glob(*)])label_filesset([f.stemforfinlabels_dir.glob(*)])missing_labelsimage_files-label_files missing_imageslabel_files-image_filesifmissing_labels:print(fMissing labels for the following images:{missing_labels})ifmissing_images:print(fMissing images for the following labels:{missing_images})else:print(All images have corresponding labels.)check_dataset(train_images,train_labels)check_dataset(val_images,val_labels)# Create YOLOv5 configuration fileconfig{train:str(train_images),val:str(val_images),nc:6,names:[apple,banana,guava,lime,orange,pomegranate]}withopen(base_path/fruits.yaml,w)asf:yaml.dump(config,f)print(Dataset checked and YOLOv5 config created.)模型定义与训练目标检测 (YOLOv5)我们将使用YOLOv5进行目标检测。以下是训练脚本train_detection.py:[titleTraining Script for Fruit Detection using YOLOv5]importsubprocess# Run YOLOv5 training commandcommand[python,train.py,--img,256,# Image size--batch,16,# Batch size--epochs,50,# Number of epochs--data,../datasets/fruits/fruits.yaml,# Path to dataset YAML file--cfg,./models/yolov5s.yaml,# Model configuration--weights,yolov5s.pt,# Pretrained weights--project,../runs/train,# Project directory--name,fruit_detection# Experiment name]subprocess.run(command)图像分类 (ResNet50)我们将使用ResNet50进行图像分类以确定水果的质量类别。以下是训练脚本train_classification.py:[titleTraining Script for Fruit Quality Classification using ResNet50]importnumpyasnpimporttensorflowastffromtensorflow.keras.applications.resnet50importResNet50,preprocess_inputfromtensorflow.keras.layersimportDense,GlobalAveragePooling2Dfromtensorflow.keras.modelsimportModelfromtensorflow.keras.optimizersimportAdamfromtensorflow.keras.preprocessing.imageimportImageDataGeneratorfromtensorflow.keras.callbacksimportModelCheckpoint,EarlyStopping# Pathstrain_dirdatasets/fruits/train/imagesvalidation_dirdatasets/fruits/val/images# Data generatorsdatagenImageDataGenerator(preprocessing_functionpreprocess_input)train_generatordatagen.flow_from_directory(train_dir,target_size(256,256),batch_size32,class_modebinary)validation_generatordatagen.flow_from_directory(validation_dir,target_size(256,256),batch_size32,class_modebinary)# Load ResNet50 model without top layerbase_modelResNet50(weightsimagenet,include_topFalse,input_shape(256,256,3))# Add custom layers on topxbase_model.output xGlobalAveragePooling2D()(x)xDense(1024,activationrelu)(x)predictionsDense(1,activationsigmoid)(x)# Combine with base modelmodelModel(inputsbase_model.input,outputspredictions)# Freeze convolutional baseforlayerinbase_model.layers:layer.trainableFalse# Compile the modelmodel.compile(optimizerAdam(lr0.0001),lossbinary_crossentropy,metrics[accuracy])# CallbackscheckpointModelCheckpoint(best_quality_model.h5,monitorval_loss,save_best_onlyTrue,modemin)early_stoppingEarlyStopping(monitorval_loss,patience10,restore_best_weightsTrue)# Train the modelhistorymodel.fit(train_generator,steps_per_epochtrain_generator.samples//train_generator.batch_size,validation_datavalidation_generator,validation_stepsvalidation_generator.samples//validation_generator.batch_size,epochs50,callbacks[checkpoint,early_stopping],verbose1)# Save training historynp.save(quality_training_history.npy,history.history)评估与可视化目标检测评估使用YOLOv5自带的评估脚本来评估目标检测模型。[titleEvaluation Script for Fruit Detection using YOLOv5]importsubprocess# Run YOLOv5 evaluation commandcommand[python,val.py,--data,../datasets/fruits/fruits.yaml,# Path to dataset YAML file--weights,../runs/train/fruit_detection/weights/best.pt,# Path to best model weights--img,256,# Image size--conf,0.5,# Confidence threshold--iou-thres,0.45,# IoU threshold--task,val,# Task type--save-json,# Save JSON results--project,../runs/val,# Project directory--name,fruit_detection_val# Experiment name]subprocess.run(command)图像分类评估编写评估脚本evaluate_classification.py来计算准确率、混淆矩阵和其他指标并绘制相应的图表。[titleEvaluation Script for Fruit Quality Classification]importnumpyasnpimportmatplotlib.pyplotaspltfromsklearn.metricsimportclassification_report,confusion_matriximportseabornassnsimporttensorflowastffromtensorflow.keras.preprocessing.imageimportImageDataGenerator# Pathsvalidation_dirdatasets/fruits/val/images# Data generatordatagenImageDataGenerator(preprocessing_functionpreprocess_input)validation_generatordatagen.flow_from_directory(validation_dir,target_size(256,256),batch_size32,class_modebinary,shuffleFalse)# Load the best modelmodeltf.keras.models.load_model(best_quality_model.h5)# Predictionsy_predmodel.predict(validation_generator)y_pred_classes(y_pred0.5).astype(int)# True labelsy_truevalidation_generator.classes# Classification reportclass_reportclassification_report(y_true,y_pred_classes,target_names[Poor Quality,Good Quality])print(class_report)# Confusion matrixconf_matconfusion_matrix(y_true,y_pred_classes)plt.figure(figsize(8,6))sns.heatmap(conf_mat,annotTrue,fmtd,cmapBlues,xticklabels[Poor Quality,Good Quality],yticklabels[Poor Quality,Good Quality])plt.xlabel(Predicted Label)plt.ylabel(True Label)plt.title(Confusion Matrix)plt.savefig(quality_confusion_matrix.png)plt.show()# Training historyhistorynp.load(quality_training_history.npy,allow_pickleTrue).item()plt.figure(figsize(12,4))plt.subplot(1,2,1)plt.plot(history[loss],labelTrain Loss)plt.plot(history[val_loss],labelValidation Loss)plt.legend(locupper right)plt.title(Loss)plt.subplot(1,2,2)plt.plot(history[accuracy],labelTrain Accuracy)plt.plot(history[val_accuracy],labelValidation Accuracy)plt.legend(loclower right)plt.title(Accuracy)plt.tight_layout()plt.savefig(quality_training_history.png)plt.show()用户界面我们将使用 PyQt5 创建一个简单的 GUI 来加载和运行模型进行实时预测。以下是用户界面脚本ui.py:[titlePyQt5 Main Window for Fruit Detection and Quality Classification]importsysimportcv2importnumpyasnpfromPyQt5.QtWidgetsimportQApplication,QMainWindow,QLabel,QPushButton,QVBoxLayout,QWidget,QFileDialogfromPyQt5.QtGuiimportQImage,QPixmapfromPyQt5.QtCoreimportQt,QTimerimporttensorflowastffromtensorflow.keras.applications.resnet50importpreprocess_inputimportultralytics.yolo.engine.modelfromPILimportImageDraw,ImageFont# Load modelsdetection_modelultralytics.yolo.engine.model.Model(cfg./models/yolov5s.yaml).load(runs/train/fruit_detection/weights/best.pt)classification_modeltf.keras.models.load_model(best_quality_model.h5)classMainWindow(QMainWindow):def__init__(self):super().__init__()self.setWindowTitle(Fruit Detection and Quality Classification System)self.setGeometry(100,100,800,600)self.initUI()definitUI(self):self.central_widgetQWidget()self.setCentralWidget(self.central_widget)self.layoutQVBoxLayout()self.image_labelQLabel(self)self.image_label.setAlignment(Qt.AlignCenter)self.layout.addWidget(self.image_label)self.load_image_buttonQPushButton(Load Image,self)self.load_image_button.clicked.connect(self.load_image)self.layout.addWidget(self.load_image_button)self.start_prediction_buttonQPushButton(Start Prediction,self)self.start_prediction_button.clicked.connect(self.start_prediction)self.layout.addWidget(self.start_prediction_button)self.stop_prediction_buttonQPushButton(Stop Prediction,self)self.stop_prediction_button.clicked.connect(self.stop_prediction)self.layout.addWidget(self.stop_prediction_button)self.central_widget.setLayout(self.layout)self.image_pathNoneself.timerQTimer()self.timer.timeout.connect(self.update_frame)defload_image(self):optionsQFileDialog.Options()file_name,_QFileDialog.getOpenFileName(self,QFileDialog.getOpenFileName(),,Images (*.png *.jpg *.jpeg);;All Files (*),optionsoptions)iffile_name:self.image_pathfile_name self.display_image(file_name)defdisplay_image(self,path):pixmapQPixmap(path)scaled_pixmappixmap.scaled(self.image_label.width(),self.image_label.height(),Qt.KeepAspectRatio)self.image_label.setPixmap(scaled_pixmap)defstart_prediction(self):ifself.image_pathisnotNoneandnotself.timer.isActive():self.timer.start(30)# Update frame every 30 msdefstop_prediction(self):ifself.timer.isActive():self.timer.stop()self.image_label.clear()defupdate_frame(self):original_imagecv2.imread(self.image_path)image_rgbcv2.cvtColor(original_image,cv2.COLOR_BGR2RGB)# Detectiondetection_resultsdetection_model(image_rgb,size256)[0].boxes.data.cpu().numpy()forresultindetection_results:x1,y1,x2,y2,conf,clsresult x1,y1,x2,y2int(x1),int(y1),int(x2),int(y2)clsint(cls)# Draw bounding boxcv2.rectangle(image_rgb,(x1,y1),(x2,y2),(0,255,0),2)# Get quality predictioncropimage_rgb[y1:y2,x1:x2]crop_resizedcv2.resize(crop,(256,256))crop_preprocessedpreprocess_input(np.expand_dims(crop_resized,axis0))quality_predclassification_model.predict(crop_preprocessed)quality_labelGood Qualityifquality_pred0.5elsePoor Quality# Put textfontcv2.FONT_HERSHEY_SIMPLEX cv2.putText(image_rgb,f{detection_model.names[cls]}({quality_label}),(x1,y1-10),font,0.9,(0,255,0),2)h,w,chimage_rgb.shape bytes_per_linech*w qt_imageQImage(image_rgb.data,w,h,bytes_per_line,QImage.Format_RGB888)pixmapQPixmap.fromImage(qt_image)scaled_pixmappixmap.scaled(self.image_label.width(),self.image_label.height(),Qt.KeepAspectRatio)self.image_label.setPixmap(scaled_pixmap)if__name____main__:appQApplication(sys.argv)windowMainWindow()window.show()sys.exit(app.exec_())请确保将路径替换为您实际的路径。使用说明配置路径确保datasets/fruits目录结构正确并且包含train和val子目录。确保runs/train/fruit_detection/weights/best.pt是训练好的 YOLOv5 模型权重路径。确保best_quality_model.h5是训练好的 ResNet50 模型权重路径。运行脚本在终端中运行data_preparation.py脚本来检查数据集的有效性并创建 YOLOv5 配置文件。在终端中运行train_detection.py脚本来训练目标检测模型。在终端中运行train_classification.py脚本来训练图像分类模型。在终端中运行evaluate_detection.py来评估目标检测模型性能。在终端中运行evaluate_classification.py来评估图像分类模型性能。在终端中运行ui.py来启动 GUI 应用程序。注意事项确保所有必要的工具箱已安装特别是 TensorFlow 和 PyQt5。根据需要调整参数如epochs和batch_size。示例假设您的数据文件夹结构如下datasets/ └── fruits/ ├── train/ │ ├── images/ │ └── labels/ └── val/ ├── images/ └── labels/并且每个数据集中包含相应的图像和标签文件。运行ui.py后您可以点击按钮来加载图像并进行水果检测和质量分类。