from tensorflow.keras.datasets import mnist from myLibrary.util import one_hot import numpy as np np.set_printoptions(edgeitems=256) (train_images, train_labels), (test_images, test_labels) = mnist.load_data() train_images = train_images.reshape(-1, 28, 28, 1) train_images = train_images / 255 train_labels = one_hot(train_labels, 10) from tensorflow.keras import layers, models model = models.Sequential() model.add( layers.Conv2D(64, kernel_size=3, activation='relu', input_shape=(28, 28, 1))) model.add(layers.MaxPool2D(pool_size=2)) model.add(layers.Flatten()) model.add(layers.Dense(10, activation='softmax')) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc']) model.fit(x=train_images, y=train_labels, batch_size=1000, epochs=30, validation_split=0.2)
for files in os.listdir(db): if files != 'label' and os.path.isdir(db + files + '/'): for img in os.listdir(db + files + '/'): X.append(db + files + '/' + img) Y.append(int(files)) return X, Y trans = Transformer() trans.add(lambda img: cv2.resize(img, (224, 224))) trans.add(lambda img: cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) DB = 'animal_train_data/' imgs, labels = read_data(DB) labels = one_hot(labels, 4) dataset = ImageDataset(imgs, labels, transformer=trans) from tensorflow.keras import metrics, optimizers net = VGG16(weights='imagenet', include_top=True) ''' net = models.Sequential() net.add(layers.Flatten()) net.add(layers.Dense(4096, activation='relu')) net.add(layers.Dense(4096, activation='relu')) net.add(layers.Dense(3, activation='softmax')) net = Model(input = pretrained.input, output = net)'''
from tensorflow.keras.datasets import mnist from tensorflow.keras.models import load_model from myLibrary.util import one_hot (train_images, train_labels), (test_images, test_labels) = mnist.load_data() test_images = test_images.reshape(-1, 28, 28, 1) test_images = test_images / 255 test_labels = one_hot(test_labels, 10) net = load_model('mnist_cnn.h5') predict = net.predict(test_images) print(predict.argmax(axis=1)) print(test_labels.argmax(axis=1)) count = (predict.argmax(axis=1) == test_labels.argmax(axis=1)).sum() print(count / len(test_images))
'''import matplotlib.pyplot as plt img = train_images[0] plt.imshow(img) plt.show() ''' tags = ['飛機', '汽車', '鳥', '貓', '鹿', '狗', '青蛙', '馬', '船', '卡車'] print(train_images.shape) train_images = train_images / 255 test_images = test_images / 255 print(train_images.shape) train_labels = one_hot(train_labels, 10) test_labels = one_hot(test_labels, 10) print(type(test_images)) from myLibrary.model import to_cifar10 net = to_cifar10(10) net.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc']) net.fit(x=train_images, y=train_labels, batch_size=1000, epochs=20, validation_split=0.2)