本文主要是介绍Dog Breed Identification,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
多分类问题
我的方案:
import pandas as pd
import numpy as np
from sklearn.utils import shuffle
import cv2
from keras.preprocessing.image import load_img,img_to_array
from keras.models import Sequential
from keras.models import load_model
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from keras.applications import VGG19
from keras.callbacks import EarlyStoppinglabels_df=pd.read_csv('./input/labels.csv')
#print(labels_df['breed'].value_counts())
#print(labels_df['breed'].describe())targets_series=pd.Series(labels_df['breed'])
one_hot=pd.get_dummies(targets_series,sparse=True)
one_hot_labels=np.asarray(one_hot)
num_classes=len(labels_df['breed'].unique())
IMAGE_SIZE=224train_img_path=['./input/train/{}.jpg'.format(item) for item in labels_df.id]#读取图片
def load_batch_image(img_path,train_set=True,target_size=(IMAGE_SIZE,IMAGE_SIZE)):im=cv2.imread(img_path)im=cv2.resize(im,target_size)if train_set:return img_to_array(im)else:return img_to_array(im)/255.0#建立一个数据迭代器
def get_dataset_shuffle(X_samples,labels,batch_size,train_set=True):X_samples,labels=shuffle(X_samples,labels)batch_num=int(len(X_samples)/batch_size)max_len=batch_num*batch_sizeX_samples=np.array(X_samples[:max_len])y_samples=labels[:max_len]X_batches=np.split(X_samples,batch_num)y_batches=np.split(y_samples,batch_num)for i in range(len(X_batches)):if train_set:x=np.array(list(map(load_batch_image,X_batches[i],[True for _ in range(batch_size)])))else:x = np.array(list(map(load_batch_image, X_batches[i], [False for _ in range(batch_size)])))y=np.array(y_batches[i])yield x,y#数据增强处理
train_datagen = ImageDataGenerator(rescale=1. / 255,rotation_range=10,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)def build_model():vgg16_net=VGG19(weights='imagenet',include_top=False,input_shape=(IMAGE_SIZE,IMAGE_SIZE,3))vgg16_net.trainable=Falsemodel=Sequential()model.add(vgg16_net)model.add(Flatten())model.add(Dense(512,activation='relu'))model.add(Dropout(0.5))model.add(Dense(num_classes,activation='softmax'))# First: train only the top layers (which were randomly initialized)for layer in model.layers:layer.trainable = Falsemodel.compile(loss='binary_crossentropy',optimizer=Adam(lr=1e-5),metrics=['accuracy'])return modeldef train():model=build_model()random_seed=2# Split the train and the validation set for the fittingtrain_X, val_X,train_y,val_y = train_test_split(train_img_path,one_hot_labels, test_size=0.1, random_state=random_seed)n_epoch = 20batch_size = 16for e in range(n_epoch):print('epoch', e)batch_num = 0loss_sum = np.array([0.0, 0.0])for X_train, y_train in get_dataset_shuffle(train_X,train_y, batch_size, True):for X_batch, y_batch in train_datagen.flow(X_train, y_train, batch_size=batch_size):loss=model.train_on_batch(X_batch,y_batch)loss_sum+=lossbatch_num+=1breakif batch_num%200==0:print("epoch %s, batch %s: train_loss = %.4f, train_acc = %.4f" % (e, batch_num, loss_sum[0] / 200, loss_sum[1] / 200))loss_sum = np.array([0.0, 0.0])res = model.evaluate_generator(get_dataset_shuffle(val_X, val_y,batch_size, False), int(len(val_X) / batch_size))print("val_loss = %.4f, val_acc = %.4f: " % (res[0], res[1]))model.save('weight.h5')def test():model=load_model('weight.h5')test_df=pd.read_csv('./input/sample_submission.csv')X_test_path=['./input/test/{}.jpg'.format(item) for item in test_df['id'] ]preds=[]for path in X_test_path:X_test=np.array(load_batch_image(path,False))X_test=np.expand_dims(X_test,axis=0)preds.append(model.predict(X_test))preds=np.squeeze(np.asarray(preds))sub = pd.DataFrame(preds)col_names = one_hot.columns.valuessub.columns = col_namessub.insert(0, 'id', test_df['id'])sub.head()
这篇关于Dog Breed Identification的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!