x = tf.io.read_file(x) x = tf.image.decode_jpeg(x, channels=3) x = tf.image.resize(x, [244, 244]) x = tf.image.random_flip_left_right(x) x = tf.image.random_crop(x, [224, 224, 3]) x = tf.cast(x, dtype=tf.float32) / 255.0 x = normalize(x) y = tf.convert_to_tensor(y) y = tf.one_hot(y, depth=num_classes) return x, y # 数据读取 train_images, train_labels, name2label = load_pokemon(data_path, 'train') train_dataloader = tf.data.Dataset.from_tensor_slices( (train_images, train_labels)) train_dataloader = train_dataloader.shuffle( len(train_images)).map(preprocess).batch(batch_size) val_images, val_labels, name2label = load_pokemon(data_path, 'val') val_dataloader = tf.data.Dataset.from_tensor_slices((val_images, val_labels)) val_dataloader = val_dataloader.map(preprocess).batch(batch_size) test_images, test_labels, name2label = load_pokemon(data_path, 'test') test_dataloader = tf.data.Dataset.from_tensor_slices( (test_images, test_labels)) test_dataloader = test_dataloader.map(preprocess).batch(batch_size) # 网络搭建
x = tf.image.random_flip_left_right(x) # x = tf.image.random_flip_up_down(x) x = tf.image.random_crop(x, [224,224,3]) # x: [0,255]=> -1~1 x = tf.cast(x, dtype=tf.float32) / 255. x = normalize(x) y = tf.convert_to_tensor(y) y = tf.one_hot(y, depth=5) return x, y batchsz = 8 # creat train db 一般训练的时候需要shuffle。其它是不需要的。 images, labels, table = load_pokemon('/Users/wzk/PythonCode/MyPy01/宝可梦/pokeman',mode='train') db_train = tf.data.Dataset.from_tensor_slices((images, labels)) # 变成个Dataset对象。 db_train = db_train.shuffle(1000).map(preprocess).batch(batchsz) # map函数图片路径变为内容。 # crate validation db images2, labels2, table = load_pokemon('/Users/wzk/PythonCode/MyPy01/宝可梦/pokeman',mode='val') db_val = tf.data.Dataset.from_tensor_slices((images2, labels2)) db_val = db_val.map(preprocess).batch(batchsz) # create test db images3, labels3, table = load_pokemon('/Users/wzk/PythonCode/MyPy01/宝可梦/pokeman',mode='test') db_test = tf.data.Dataset.from_tensor_slices((images3, labels3)) db_test = db_test.map(preprocess).batch(batchsz) # 训练样本太小了,resnet网络表达能力很强。这里换成4层小的网络了。 resnet = keras.Sequential([ layers.Conv2D(16,5,3),
x = tf.image.random_flip_left_right(x) x = tf.image.random_flip_up_down(x) x = tf.image.random_crop(x, [224, 224, 3]) # x: [0,255]=> -1~1 x = tf.cast(x, dtype=tf.float32) / 255. x = normalize(x) y = tf.convert_to_tensor(y) y = tf.one_hot(y, depth=5) return x, y batchsz = 32 # 创建训练集Datset对象 images, labels, table = load_pokemon('pokemon', mode='train') db_train = tf.data.Dataset.from_tensor_slices((images, labels)) db_train = db_train.shuffle(1000).map(preprocess).batch(batchsz) # 创建验证集Datset对象 images2, labels2, table = load_pokemon('pokemon', mode='val') db_val = tf.data.Dataset.from_tensor_slices((images2, labels2)) db_val = db_val.map(preprocess).batch(batchsz) # 创建测试集Datset对象 images3, labels3, table = load_pokemon('pokemon', mode='test') db_test = tf.data.Dataset.from_tensor_slices((images3, labels3)) db_test = db_test.map(preprocess).batch(batchsz) # 加载DenseNet网络模型,并去掉最后一层全连接层,最后一个池化层设置为max pooling net = keras.applications.DenseNet121(weights='imagenet', include_top=False, pooling='max')