Keras using Conv2D for MINST

Posted peterwarlg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keras using Conv2D for MINST相关的知识,希望对你有一定的参考价值。

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]       $	o$          [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的主要内容,如果未能解决你的问题,请参考以下文章

keras.layers.Conv2D中过滤器参数中keras使用了哪些类型的“过滤器”?

Keras 中的 conv2d 和 Conv2D 有啥区别?

keras.layers.Conv2D 的默认内核大小、零填充和步幅是多少?

如何正确从 keras 中的 Conv2D 获取层权重?

keras.layers.Conv2D的默认内核大小,零填充和步幅是什么?

如何在 Conv2D 层中使用 keras 指定填充?