码迷,mamicode.com
首页 > 其他好文 > 详细

Keras using Conv2D for MINST

时间:2020-10-31 01:42:28      阅读:13      评论:0      收藏:0      [点我收藏+]

标签:conv2   config   ace   打印   rbo   att   option   models   options   

import


import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D

数据处理

输入的data

x_train = x_train.reshape(
x_train.shape[0],      #数据个数
28,28,1              #单组数据  28*28的灰度图
)

数据标签

一、                  二、
[                      [
[1]                      [1,0,0]
[2]       $\to$          [0,1,0]
[3]                      [0,0,1]
]                                ]

loss:
左:sparse_categorical_crossentropy
右:categorical_crossentropy

创建Sequential模型

全连接

Conv模型

model_name = Sequential
model_name .add(Conv2D(32, kernel_size=(3, 3),
                 activation=‘relu‘,
                 input_shape=(28,28,1)))
model_name .add(MaxPooling2D(pool_size=(2, 2)))
model_name .add(Dense(64, activation=‘relu‘))
model_name .add(Dense(64, activation=‘relu‘))
model_name .add(Flatten())
model_name .add(Dense(64, activation=‘relu‘))
model_name .add(Dense(10, activation=‘softmax‘))

model_name .summary()   #打印model信息

Train Model

model.compile(loss=tf.keras.losses.categorical_crossentropy,
              optimizer=tf.keras.optimizers.Adam(),
              metrics=[‘accuracy‘])

或者

sgd=tf.keras.optimizers.SGD(lr=0.01,decay=1e-6,momentum=0.9,nesterov=True)
model.compile(loss=‘categorical_crossentropy‘,
              optimizer=sgd,
              metrics=[‘accuracy‘]
              )

model.fit(x_train, y_train, batch_size=128, epochs=5, verbose=1)

模型的保存和读取

from tensorflow.keras.models import load_model
#保存
model_save_path = "model_name.h5"
model.save(model_save_path)
#读取
model_save_path = "model_name.h5"
load_model(model_save_path)
predict_data = model.predict(train_data)

GPU和CPU的调用

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"    #-1 为CPU;  0,1为GPU

报错合集

在使用Conv2D时,Failed to get convolution algorithm.

自动分配GPU

config = tf.compat.v1.ConfigProto(allow_soft_placement=True)
config.gpu_options.per_process_gpu_memory_fraction = 0.3
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

ALL Code(Minst)

训练

from __future__ import print_function
import numpy as np
import pandas as pd
# import os
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
import tensorflow as tf

config = tf.compat.v1.ConfigProto(allow_soft_placement=True)
config.gpu_options.per_process_gpu_memory_fraction = 0.3
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

p = r‘train_label.csv‘
q = r‘train.csv‘

with open(p, encoding=‘utf-8‘) as f:
    y_train = np.loadtxt(f, delimiter=",", skiprows=1)
y_train = y_train[:, np.newaxis]
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)

with open(q, encoding=‘utf-8‘) as f:
    x_train = np.loadtxt(f, delimiter=",", skiprows=1)

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_train /= 255

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation=‘relu‘,
                 input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dense(64, activation=‘relu‘))
model.add(Dense(64, activation=‘relu‘))
model.add(Flatten())
model.add(Dense(64, activation=‘relu‘))
model.add(Dense(10, activation=‘softmax‘))

model.summary()

model.compile(loss=tf.keras.losses.categorical_crossentropy,
              optimizer=tf.keras.optimizers.Adam(),
              metrics=[‘accuracy‘])

model.fit(x_train, y_train, batch_size=128, epochs=5, verbose=1)

# model_save_path = "model1.h5"
# model.save(model_save_path)

测试

from __future__ import print_function
import numpy as np
import pandas as pd
import os
import tensorflow as tf

config = tf.compat.v1.ConfigProto(allow_soft_placement=True)
config.gpu_options.per_process_gpu_memory_fraction = 0.3
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

from tensorflow.keras.models import load_model
model_save_path = "model1.h5"
model = load_model(model_save_path)

p = r‘test.csv‘
with open (p,encoding=‘utf-8‘) as f:
    x_test = np.loadtxt(f,delimiter=",",skiprows=1)

x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
x_test /= 255

y_pred = model.predict(x_test)
Label = np.argmax(y_pred,axis=1)
print(Label)

Keras using Conv2D for MINST

标签:conv2   config   ace   打印   rbo   att   option   models   options   

原文地址:https://www.cnblogs.com/peterwarlg/p/13887277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!