将CIFAR10数据集拆分为train、val以及test

import cv2
import  numpy as np
from pathlib import Path
from tqdm import tqdm

import pickle

#先读标签
with open('cifar-10-batches-py/batches.meta', 'rb') as fo:
	dict_meta = pickle.load(fo, encoding='bytes')
label_names = dict_meta[b'label_names']

#训练集和验证集
single_train_number = 9000
train_paths = ['cifar-10-batches-py/data_batch_1','cifar-10-batches-py/data_batch_2','cifar-10-batches-py/data_batch_3',
				'cifar-10-batches-py/data_batch_4','cifar-10-batches-py/data_batch_5']
				
for i in tqdm(range(len(train_paths))):
	with open(train_paths[i], 'rb') as fo:
		dict = pickle.load(fo, encoding='bytes')
		
	for j in tqdm(range(single_train_number-1)):
		path = Path("./cifar-10-batches-py/train/")
		tag = str(label_names[dict[b'labels'][j]])+'_'+str(dict[b'labels'][j])
		path = path/tag
		if path.exists()==False :
			path.mkdir(parents=True)
		image_path = "./cifar-10-batches-py/train/"+tag+'/'+tag+'_'+str((i+1)*j)+'.jpg'
		a = dict[b'data'][j]
		b = a[0:1024].reshape((32,32),order='C')[:,:,np.newaxis]
		c = a[1024:2048].reshape((32,32),order='C')[:,:,np.newaxis]
		d = a[2048:3072].reshape((32,32),order='C')[:,:,np.newaxis]
		img = np.concatenate((b,c,d),axis = 2)
		cv2.imwrite(image_path, img)
		
	for j in tqdm(range(len(dict[b'data'])-single_train_number)):
		path = Path("./cifar-10-batches-py/val/")
		j = j + single_train_number
		tag = str(label_names[dict[b'labels'][j]])+'_'+str(dict[b'labels'][j])
		path = path/tag
		if path.exists()==False :
			path.mkdir(parents=True)
		image_path = "./cifar-10-batches-py/val/"+tag+'/'+tag+'_'+str((i+1)*j)+'.jpg'
		a = dict[b'data'][j]
		b = a[0:1024].reshape((32,32),order='C')[:,:,np.newaxis]
		c = a[1024:2048].reshape((32,32),order='C')[:,:,np.newaxis]
		d = a[2048:3072].reshape((32,32),order='C')[:,:,np.newaxis]
		img = np.concatenate((b,c,d),axis = 2)
		cv2.imwrite(image_path, img)

#测试集
test_path = 'cifar-10-batches-py/test_batch'

with open(test_path, 'rb') as fo:
	dict = pickle.load(fo, encoding='bytes')
		
for j in tqdm(range(len(dict[b'labels']))):
	path = Path("./cifar-10-batches-py/test/")
	tag = str(label_names[dict[b'labels'][j]])+'_'+str(dict[b'labels'][j])
	path = path/tag
	if path.exists()==False :
		path.mkdir(parents=True)
	image_path = "./cifar-10-batches-py/test/"+tag+'/'+tag+'_'+str((i+1)*j)+'.jpg'
	a = dict[b'data'][j]
	b = a[0:1024].reshape((32,32),order='C')[:,:,np.newaxis]
	c = a[1024:2048].reshape((32,32),order='C')[:,:,np.newaxis]
	d = a[2048:3072].reshape((32,32),order='C')[:,:,np.newaxis]
	img = np.concatenate((b,c,d),axis = 2)
	cv2.imwrite(image_path, img)

 

以下是在CIFAR10数据集上实现EfficientNet模型的代码: ```python import tensorflow as tf from tensorflow.keras.layers import Conv2D, Dropout, BatchNormalization, Dense, GlobalAveragePooling2D from tensorflow.keras.models import Sequential from tensorflow.keras.regularizers import l2 from tensorflow.keras.optimizers import Adam from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau from efficientnet.tfkeras import EfficientNetB0 # 加载CIFAR10数据集 (x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data() # 数据预处理 x_train = x_train.astype('float32') / 255. x_test = x_test.astype('float32') / 255. y_train = tf.keras.utils.to_categorical(y_train, num_classes=10) y_test = tf.keras.utils.to_categorical(y_test, num_classes=10) # 创建EfficientNet模型 model = Sequential() model.add(EfficientNetB0(input_shape=(32, 32, 3), weights=None, include_top=False)) model.add(GlobalAveragePooling2D()) model.add(Dense(10, activation='softmax')) # 编译模型 model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy']) # 训练模型 checkpoint = ModelCheckpoint('efficientnet.h5', monitor='val_loss', save_best_only=True) earlystop = EarlyStopping(monitor='val_loss', patience=10) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5) callbacks = [checkpoint, earlystop, reduce_lr] history = model.fit(x_train, y_train, batch_size=128, epochs=100, validation_data=(x_test, y_test), callbacks=callbacks) ``` 这段代码使用EfficientNetB0作为特征提取器,然后添加全局平均池化层和一个全连接层。编译模型使用Adam优化器和分类交叉熵损失函数。在训练期间,使用了ModelCheckpoint、EarlyStopping和ReduceLROnPlateau回调函数来优化模型的训练过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值