FlyAI竞赛使用tensorflow2.0进行海底物种分类

这是FlyAI的一个数据竞赛题目,使用tensorflow2.0构建卷积神经网络对海底的鱼类物种进行分类,数据集可以在FlyAI官网进行下载。

import tensorflow as tf
import pandas as pd
import cv2
import numpy as np

def proprecess(x,y):
    x = tf.cast(x,dtype=tf.float32) / 255.0
    y = tf.cast(y,dtype=tf.int32)
    return x,y

df = pd.read_csv(r'F:\fish\FishClassification_FlyAI\data\input\FishClassification\train.csv')
img_path_list = df['image_path'].values
img_label_list = df['label'].values

path = 'F:\\fish\\FishClassification_FlyAI\\data\\input\\FishClassification\\'
print(len(img_path_list))
print(len(img_label_list))
train_x = []
train_y = []
test_x = []
test_y = []

for i in range(len(img_path_list)):
    img_path = path + img_path_list[i]
    imgs = cv2.imread(img_path,-1)
    imgs = cv2.resize(imgs,(128,128))
    if i % 10 == 0:
        test_x.append(imgs)
        test_y.append(img_label_list[i])
    else:
        train_x.append(imgs)
        train_y.append(img_label_list[i])

print(len(train_x) + len(test_x))
print(len(train_y) + len(test_y))

train_x = np.array(train_x,dtype=tf.float32)
train_y = np.array(train_y,dtype=tf.int32)
test_x = np.array(test_x,dtype=tf.float32)
test_y = np.array(test_y,dtype=tf.int32)

train_db = tf.data.Dataset.from_tensor_slices((train_x,train_y))
train_db = train_db.map(proprecess).shuffle(1000).batch(32)
test_db = tf.data.Dataset.from_tensor_slices((test_x,test_y))
test_db = test_db.map(proprecess).batch(32)

class Mymodel(tf.keras.Model):
    def __init__(self):
        super().__init__()
        self.conv1 = tf.keras.layers.Conv2D(filters=32,kernel_size=[3,3],padding='same',activation=tf.nn.relu)
        self.pool1 = tf.keras.layers.MaxPool2D(pool_size=[2,2],strides=[2,2])
        self.conv2 = tf.keras.layers.Conv2D(filters=64,kernel_size=[3,3],padding='same',activation=tf.nn.relu)
        self.pool2 = tf.keras.layers.MaxPool2D(pool_size=[2,2],strides=[2,2])
        self.flatten = tf.keras.layers.Flatten()
        self.fc1 = tf.keras.layers.Dense(64,activation=tf.nn.relu)
        self.fc2 = tf.keras.layers.Dense(21,activation=tf.nn.softmax)
    def call(self,inputs):
        x = self.conv1(inputs)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.pool2(x)
        x = self.flatten(x)
        x = self.fc1(x)
        x = self.fc2(x)
        return x

model = Mymodel()
model.build(input_shape=(None,128,128,3))
model.summary()
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)


for epoch in range(10):
    train_loss = 0
    train_num = 0
    for x,y in train_db:
        x = tf.reshape(x,[-1,128,128,3])
        with tf.GradientTape() as tape:
            pred = model(x)
            pred = tf.cast(pred,dtype=tf.int32)
            loss = tf.keras.losses.sparse_categorical_crossentropy(y_true=y,y_pred=pred)
            loss = tf.reduce_mean(loss)
        grads = tape.gradient(loss,model.trainable_variables)
        optimizer.apply_gradients(zip(grads,model.trainable_variables))
        train_loss += loss
        train_num += x.shape[0]
    loss = float(train_loss / train_num)

    total_correct = 0
    total_num = 0
    for x,y in test_db:
        x = tf.reshape(x,[-1,128,128,3])
        pred = model(x)
        pred = tf.argmax(pred,axis=1)
        pred = tf.cast(pred,dtype=tf.int32)
        correct = tf.equal(pred,y)
        correct = tf.reduce_sum(tf.cast(correct,dtype=tf.int32))
        total_correct += correct
        total_num += x.shape[0]
    accuracy = float(total_correct / total_num)
    print(epoch,'train_loss:',train_loss,'accuracy:',accuracy)

print('............................prediction..........................')
for x,y in test_db:
    img = x
    label = y
    break
img = tf.reshape(img,[-1,128,128,3])
pred = model(img)
pred = tf.argmax(pred,axis=1)
pred = tf.cast(pred,dtype=tf.int32)
print('pred:',pred)
print('label:',label)
print('The pred is equal the label',tf.equal(pred,label))

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨小嗨yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值