当前位置:   article > 正文

10分钟搭建一个简易深度学习模型_如何建造自己的深度学习模型

如何建造自己的深度学习模型

1、 安装模块
tensorflow、pillow、keras
2、数据集介绍
一个文件夹+train说明+预测海报
Images文件夹包含经过编码的海报图片
id、类型、具体类型向量
3、 代码
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout,Flatten
from keras.layers import Conv2D,MaxPooling2D
from keras.utils import to_categorical
from keras.preprocessing import image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from tqdm import tqdm
#%matplotlib inline
import os

path = “C:\Users\hydro\PycharmProjects\untitled\couhua\”
print(path)

read .csv file

train = pd.read_csv(path+“Multi_Label_dataset\train.csv”)
train.head()
train.columns

preprocessing

train_image = []
for i in tqdm(range(train.shape[0])):
img = image.load_img(path+‘Multi_Label_dataset\Images\’+train[‘Id’][i]+’.jpg’,target_size=(400,400,3))
img = image.img_to_array(img)
img = img/255
train_image.append(img)
X = np.array(train_image)
X.shape

y = np.array(train.drop([‘Id’,‘Genre’],axis=1))
y.shape
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=42,test_size=0.1)

building model

model = Sequential()
model.add(Conv2D(filters=16,kernel_size=(3,5),activation=“relu”,input_shape=(400,400,3)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=32,kernel_size=(5,5),activation=‘relu’))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation=“relu”))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation=‘relu’))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation=‘relu’))
model.add(Dropout(0.5))
model.add(Dense(64, activation=‘relu’))
model.add(Dropout(0.5))
model.add(Dense(25, activation=‘sigmoid’))

model.summary()

model.compile(optimizer=‘adam’,loss=‘binary_crossentropy’,metrics=[‘accuracy’])

model.fit(X_train,y_train,epochs=10,validation_data=(X_test,y_test),batch_size=64)

prediction

img = image.load_img(path+’\’+‘GOT.jpg’,target_size=(400,400,3))
img = image.img_to_array(img)
img = img/255

classes = np.array(train.columns[2:])
proba = model.predict(img.reshape(1,400,400,3))
top_3 = np.argsort(proba[0])[:-4:-1]
for i in range(3):
print("{}".format(classes[top_3[i]])+" ({:.3})".format(proba[0][top_3[i]]))
plt.imshow(img)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/79191
推荐阅读
相关标签
  

闽ICP备14008679号