基于Tensorflow实现苹果新鲜度分类模型

这是一个专为判断苹果是否变质或新鲜而构建的图像分类模型。

python 复制代码
import tensorflow as tf
ds_train=tf.keras.preprocessing.image_dataset_from_directory(
    '/Apple',
    labels="inferred",
    label_mode='binary',
    interpolation='nearest',
    batch_size=64,
    image_size=[128,128],
    shuffle=False
)
def convert_to_int(image,label):
    image=tf.image.convert_image_dtype(image,dtype=tf.float32)
    return image,label
ds_train=(
    ds_train.
    map(convert_to_int).
    cache()
)
model=tf.keras.Sequential([
    tf.keras.layers.Conv2D(filters=32,kernel_size=5,input_shape=[128,128,3],activation='relu',padding='same'),
    tf.keras.layers.MaxPool2D(),
    tf.keras.layers.Conv2D(filters=64,kernel_size=5,activation='relu',padding='same'),
    tf.keras.layers.MaxPool2D(),
    tf.keras.layers.Conv2D(filters=128,kernel_size=5,activation='relu',padding='same'),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(units=8,activation='relu'),
    tf.keras.layers.Dense(units=1,activation='sigmoid')]
)
model.summary()
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['binary_accuracy'])
history=model.fit(ds_train,epochs=9)
复制代码
2025-12-28 06:59:40.835848: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:467] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1766905181.010572      24 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1766905181.067789      24 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
W0000 00:00:1766905181.526749      24 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1766905181.526780      24 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1766905181.526782      24 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1766905181.526785      24 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.


Found 170 files belonging to 2 classes.


I0000 00:00:1766905195.483352      24 gpu_device.cc:2019] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 15513 MB memory:  -> device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0, compute capability: 6.0
/usr/local/lib/python3.12/dist-packages/keras/src/layers/convolutional/base_conv.py:113: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
  super().__init__(activity_regularizer=activity_regularizer, **kwargs)

Model: "sequential"

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Layer (type)                    ┃ Output Shape           ┃       Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ conv2d (Conv2D)                 │ (None, 128, 128, 32)   │         2,432 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d (MaxPooling2D)    │ (None, 64, 64, 32)     │             0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ conv2d_1 (Conv2D)               │ (None, 64, 64, 64)     │        51,264 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d_1 (MaxPooling2D)  │ (None, 32, 32, 64)     │             0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ conv2d_2 (Conv2D)               │ (None, 32, 32, 128)    │       204,928 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ flatten (Flatten)               │ (None, 131072)         │             0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense (Dense)                   │ (None, 8)              │     1,048,584 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_1 (Dense)                 │ (None, 1)              │             9 │
└─────────────────────────────────┴────────────────────────┴───────────────┘

Total params: 1,307,217 (4.99 MB)

Trainable params: 1,307,217 (4.99 MB)

Non-trainable params: 0 (0.00 B)

Epoch 1/9


WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1766905198.685538      70 service.cc:152] XLA service 0x7fcc28087d90 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
I0000 00:00:1766905198.685575      70 service.cc:160]   StreamExecutor device (0): Tesla P100-PCIE-16GB, Compute Capability 6.0
I0000 00:00:1766905199.133224      70 cuda_dnn.cc:529] Loaded cuDNN version 91002


[1m1/3[0m [32m━━━━━━[0m[37m━━━━━━━━━━━━━━[0m [1m13s[0m 7s/step - binary_accuracy: 1.0000 - loss: 0.6743

I0000 00:00:1766905203.877915      70 device_compiler.h:188] Compiled cluster using XLA!  This line is logged at most once for the lifetime of the process.


[1m3/3[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m10s[0m 2s/step - binary_accuracy: 0.8226 - loss: 1.4248
Epoch 2/9
[1m3/3[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 25ms/step - binary_accuracy: 0.1774 - loss: 0.9172    
Epoch 3/9
[1m3/3[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 25ms/step - binary_accuracy: 0.2682 - loss: 0.8156
Epoch 4/9
[1m3/3[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 25ms/step - binary_accuracy: 0.8226 - loss: 0.5356
Epoch 5/9
[1m3/3[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 25ms/step - binary_accuracy: 0.8226 - loss: 0.5897
Epoch 6/9
[1m3/3[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 25ms/step - binary_accuracy: 0.8226 - loss: 0.5844
Epoch 7/9
[1m3/3[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 24ms/step - binary_accuracy: 0.8256 - loss: 0.4883
Epoch 8/9
[1m3/3[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 24ms/step - binary_accuracy: 0.8746 - loss: 0.3659
Epoch 9/9
[1m3/3[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 25ms/step - binary_accuracy: 0.9491 - loss: 0.2893
python 复制代码
import matplotlib.pyplot as plt
plt.plot(history.history['binary_accuracy'])
复制代码
[<matplotlib.lines.Line2D at 0x7fcccc53ac60>]
相关推荐
甲维斯10 小时前
测一波MiMo 2.5 Pro,看看真实实力!
人工智能
qq_4112624210 小时前
四博 AI 双目智能音箱技术拆解
人工智能·智能音箱
xwz小王子10 小时前
Science Robotics 让机器人学会“削果皮”:一种曲面物体操作任务转移的新方法
人工智能·机器人
qq_4112624210 小时前
四博 AI 双目智能音箱方案:从“会说话”升级到“有感知、有表情、有反馈”的 AI 硬件平台
人工智能·智能音箱
xiaoduo AI10 小时前
客服机器人非工作时间能休眠?智能Agent开放平台定时唤醒,无人值守省资源?
大数据·人工智能·机器人
这张生成的图像能检测吗10 小时前
(论文速读)IMSE-IGA-CNN-Transformer
人工智能·深度学习·cnn·transformer·故障诊断·预测模型·时序模型
冬奇Lab11 小时前
RAG 系列(一):大模型为什么需要「外挂记忆」
人工智能·llm
冬奇Lab11 小时前
一天一个开源项目(第86篇):VibeVoice —— 微软开源的前沿语音 AI,单次处理 90 分钟多说话人音频
人工智能·llm
AI自动化工坊12 小时前
Hugging Face ml-intern技术深度解析:AI机器学习工程师的工程实践
人工智能·机器学习·huggingface·ml-intern·ai机器学习
疯狂成瘾者12 小时前
Agent 的需求理解质量如何具体实现:从意图识别到槽位补全、追问与确认机制
人工智能·自然语言处理