基于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>]
相关推荐
JoySSLLian9 分钟前
手把手教你安装免费SSL证书(附宝塔/Nginx/Apache配置教程)
网络·人工智能·网络协议·tcp/ip·nginx·apache·ssl
BestSongC10 分钟前
行人摔倒检测系统 - 前端文档(1)
前端·人工智能·目标检测
空白诗10 分钟前
CANN ops-nn 算子解读:Stable Diffusion 图像生成中的 Conv2D 卷积实现
深度学习·计算机视觉·stable diffusion
模型时代16 分钟前
Anthropic明确拒绝在Claude中加入广告功能
人工智能·microsoft
夕小瑶20 分钟前
OpenClaw、Moltbook爆火,算力如何48小时内扩到1900张卡
人工智能
一枕眠秋雨>o<22 分钟前
透视算力:cann-tools如何让AI性能调优从玄学走向科学
人工智能
那个村的李富贵36 分钟前
昇腾CANN跨行业实战:五大新领域AI落地案例深度解析
人工智能·aigc·cann
集简云-软件连接神器39 分钟前
技术实战:集简云语聚AI实现小红书私信接入AI大模型全流程解析
人工智能·小红书·ai客服
松☆39 分钟前
深入理解CANN:面向AI加速的异构计算架构
人工智能·架构
rainbow72424439 分钟前
无基础学AI的入门核心,从基础工具和理论开始学
人工智能