基于EfficientNetB0的车辆品牌识别

本文档在Colab环境中使用Tensorflow 2.1实现基于EfficientNetB0的车辆品牌识别。讨论了两个关键问题:1) ImageDataGenerator处理数据时类别索引的不确定性;2) os.listdir()读取文件夹内容的无序性。
摘要由CSDN通过智能技术生成

基于EfficientNetB0的车辆品牌识别

0 运行环境:Colab tensorflow 2.1

1 代码

%tensorflow_version 2.x
import tensorflow as tf
print(tf.__version__)
#from google.colab import drive
#drive.mount('/content/drive/')
import os
import pandas as pd 
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation,GlobalMaxPooling2D,GlobalAveragePooling2D
from tensorflow.keras import applications
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import optimizers
from tensorflow.keras.applications import *
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split


!pip install PyDrive googledrivedownloader
#https://drive.google.com/open?id=1K1X9-ori8zj0TdOem6dJZs2qvuiOjBVx
from google_drive_downloader import GoogleDriveDownloader
GoogleDriveDownloader.download_file_from_google_drive(file_id='1K1X9-ori8zj0TdOem6dJZs2qvuiOjBVx',dest_path='./content',unzip=True)
BATCH_SIZE = 128
IMAGE_SIZE = 224
EPOCHS = 50
INPUT_SHAPE = (IMAGE_SIZE, IMAGE_SIZE, 3)


import matplotlib.pyplot as plt
with tf.device('/gpu:0'):
  import numpy as np
  np.random.seed(2345)
  import pandas as pd
  df_raw = pd.read_csv("./train.csv")
  df = df_raw.sample(frac=1) #打乱顺序
  df["category"] = df["label"].replace({
   0: 'Audi', 2: 'benz', 1: 'BMW'})
  df = df.drop(columns="label")
  total_df = df.shape[0]


with tf.device('/gpu:0'):

  train_df, validate_df = train_test_split(df, test_size=0.1)
  #train_df = df[:np.int(total_df*0.9)]
  #validate_df = df[np.int(total_df*0.9):]
  #
  train_df = train_df.reset_index(drop=True)
  validate_df = validate_df.reset_index(drop=True)

  #validate_df = validate_df.sample(n=100).reset_index() # use for fast testing code purpose
  #train_df = train_df.sample(n=1000).reset_index() # use for fast testing code purpose

  total_train = train_df.shape[0]
  total_validate = validate_df.shape[0]

  train_datagen = ImageDataGenerator(
      rotation_range=15,
      rescale=1./255,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest',
      width_shift_range=0.1,
      height_shift_range=0.1
  )

  train_generator = train_datagen.flow_from_dataframe(
      train_df,
      "./train",
      x_col = 'filename',
      y_col = 'category',
      class_mode='categorical',
      target_size=(IMAGE_SIZE, IMAGE_SIZE),
      batch_size
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值