tensorflow2 keras 调用官方提供的模型训练分类与测试

本任务为分类分类属性的8个类别。
dict_gender = {'f':0,
'm':1
}
dict_age = {'children':0,
'young':1,
'adult':2,
'older':3
}

用的keras的datagen_train.flow_from_directory。

train_generator=datagen_train.flow_from_directory('/home/nfs/em1/train_data/age_gender/train-dir',#类别子文件夹的上一级文件夹
                                         batch_size=BATCH_SIZE,
                                         shuffle=True,
                                        target_size=[SIZE, SIZE],
                                        class_mode='categorical'
                                      )

文件夹train-dir下放8个文件夹,分别表示8类。自动制作标签。

print(train_generator.class_indices)
print(valid_generator.class_indices)

可以显示类别

resnet50

import os,sys
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing.image import ImageDataGenerator

from PIL import Image
import random

from tensorflow.keras.callbacks import ModelCheckpoint



print(tf.__version__)
print(sys.version_info)

HEIGHT = 224
WIDTH = 224

BATCH_SIZE = 64

SIZE = HEIGHT

NUM_TRAIN = 342155
NUM_VAL = 1981


model = ResNet50(
    weights=None, # weights='imagenet'  这里也可以预训练自己的模型 写模型路径即可
    classes=8
)

model.compile(optimizer=tf.train.AdamOptimizer(0.001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

print(model.summary())

#model.load_weights( "my_net.hdf5" ) #加载预训练模型继续训练

datagen_train = ImageDataGenerator(
        rescale=1./255.0,
        rotation_range=1.5,
        width_shift_range=0.05,
        height_shift_range=0.05,
        shear_range=0.1,
        zoom_range=0.1,
        horizontal_flip=True,
        fill_mode='nearest')

datagen_val = ImageDataGenerator(
        rescale=1./255.0)


train_generator=datagen_train.flow_from_directory('/home/nfs/em1/yanghailin_em1/train_data/age_gender/my_aug_chengguang/train',#类别子文件夹的上一级文件夹
                                         batch_size=BATCH_SIZE,
                                         shuffle=True,
                                        target_size=[SIZE, SIZE],
                                        class_mode='categorical'
                                      )

valid_generator=datagen_val.flow_from_directory('/home/nfs/em1/yanghailin_em1/train_data/age_gender/my_aug_chengguang/test',#类别子文件夹的上一级文件夹
                                         batch_size=BATCH_SIZE,
                                         shuffle=False,
                                        target_size=[SIZE, SIZE],
                                        class_mode='categorical'
                                      )

print(train_generator.class_indices)
print(valid_generator.class_indices)


epochs = 10000
filepath = "./model/resnet50-keras_model_{epoch:03d}-{val_acc:.4f}.h5" #避免文件名称重复
checkpoint = ModelCheckpoint(filepath=filepath, monitor='val_acc', verbose=1,
                             save_best_only=False, mode='max')
history = model.fit_generator(generator = train_generator,
                           steps_per_epoch=NUM_TRAIN // BATCH_SIZE,
                           epochs=epochs,
                           validation_data=valid_generator,
                              validation_steps=NUM_VAL // BATCH_SIZE,
                              verbose=1,callbacks=[checkpoint])

resnet50测试代码

import os,sys
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing.image import ImageDataGenerator

from PIL import Image
import random

from tensorflow.keras.callbacks import ModelCheckpoint



print(tf.__version__)
print(sys.version_info)

HEIGHT = 224
WIDTH = 224

BATCH_SIZE = 64

SIZE = HEIGHT

NUM_TRAIN = 342155
NUM_VAL = 1981





model = ResNet50(
    weights=None,
    classes=8
)

model.compile(optimizer=tf.train.AdamOptimizer(0.001),
             loss='categorical_crossentropy',
             metrics=['accuracy'])

print(model.summary())

model.load_weights("/resnet50/fuwuqi/model/resnet50-keras_model_017-0.8536.h5")


dict_label_tijiao = {"f_children":"0",
              "f_young":"1",
              "f_adult":"2",
              "f_older":"3",
              "m_children":"4",
              "m_young":"5",
              "m_adult":"6",
              "m_older":"7"
              }

own_label = {'f_adult': 0, 'f_children': 1, 'f_older': 2, 'f_young': 3, 'm_adult': 4, 'm_children': 5, 'm_older': 6, 'm_young': 7}
map_own_label2tijiao = {0:'f_adult',1:'f_children',2:'f_older',3:'f_young',4:'m_adult',5:'m_children',6:'m_older',7:'m_young'}



root_dir_test =  "/data_2/big-data/compete/20200323/src_data/test-tijiao/"
with open(root_dir_test + 'result.txt','w')as fw:
    for root, dirs, files in os.walk(root_dir_test):
        if 0 == len(files):
            continue
        for img_name_ in files:
            print(img_name_)
            if img_name_.endswith(".jpg") or img_name_.endswith(".jpeg") or img_name_.endswith(".png"):
                pos = img_name_.find(".")
                name = img_name_[0:pos]
                img_path = os.path.join(root,img_name_)
                img = image.load_img(img_path, target_size=(SIZE, SIZE))
                img = image.img_to_array(img) / 255.0
                img = np.expand_dims(img, axis=0)  # 为batch添加第四维
                predictions = model.predict(img)
                label = np.argmax(predictions, axis=1)
                print(predictions)
                print("label=", label)
                print('*' * 100)
                # cv2.imshow("img", img[0][:, :, ::-1])
                # cv2.waitKey(0)

上一篇:tensorflow2使用全连接神经探讨电影评论分类及模型优化


下一篇:tensorflow2.AutoGraph