Use
> tf.keras.optimizers.Adam(learning_rate)
instead of
> keras.optimizers.Adam(learning_rate)
www.marearts.com
Showing posts with label Keras. Show all posts
Showing posts with label Keras. Show all posts
2/15/2022
2/14/2022
cannot import name 'to_categorical' from 'keras.utils' (/Users/leon/DWF/Dev/FindWalli/find_waldo/env_fw/lib/python3.8/site-packages/keras/utils/__init__.py)
use this
> from tensorflow.keras.utils import to_categorical
instead of this
> from keras.utils import to_categorical
Thank you.
www.marearts.com
12/08/2019
keras model summary, print out for each layers property
from keras.layers import Input
from keras.applications import VGG16
vgg_model = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3))) #the head FC layer off
#how many layers
print(len(vgg_model.layers))
#last layer
print(vgg_model.layers[-1].name)
#print whole layer
for idx, layer in enumerate(vgg_model.layers):
print(idx+1, '-----')
print(layer.output_shape)
print(layer.name)
print('--------')
#summary of model
vgg_model.summary()

7/30/2019
Simple example for CNN + MNIST + Keras, Tensorboard, save model, load model
Training Code CNN + MNIST
..
..
CNN network Layout
Dataset
Run Tensorboard
Load Model and test one mnist image
...
...
Test image
output
[7]
download minist jpeg file on here: http://study.marearts.com/2015/09/mnist-image-data-jpg-files.html
..
import keras from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten, Input from keras.layers import Conv2D, MaxPooling2D"""Build CNN Model"""num_classes = 10 input_shape = (28, 28, 1) #mnist channels first format model = Sequential() model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) model.add(Conv2D(64, (3, 3), 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(num_classes, activation='softmax')) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) model.summary() """Download MNIST Data""" from keras.datasets import mnist import numpy as np # the data, split between train and test sets (x_train, y_train), (x_test, y_test) = mnist.load_data() #(60000, 28, 28) -> (60000, 28, 28, 1) x_train = x_train.astype('float32') / 255. x_test = x_test.astype('float32') / 255. x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format # convert class vectors to binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) """Show some images""" import matplotlib.pyplot as plt row = 10 col = 10 n = row * col plt.figure(figsize=(4, 4)) for i in range(n): # display original #https://jakevdp.github.io/PythonDataScienceHandbook/04.08-multiple-subplots.html ax = plt.subplot(row, col, i+1) plt.imshow(x_test[i].reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() """set up tensorboard""" from datetime import datetime import os logdir="logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S") os.makedirs(logdir, exist_ok=True) tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir) """Train model""" from keras.callbacks import TensorBoard batch_size = 128 epochs = 1 model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, shuffle=True, validation_data=(x_test, y_test), callbacks=[TensorBoard(log_dir=logdir)]) score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) """test one image data """ x_test[0].shape one_image = x_test[0].reshape(1,28,28,1) y_pred_all = model.predict(one_image) y_pred_it = model.predict_classes(one_image) print(y_pred_all, y_pred_it) plt.imshow(x_test[0].reshape(28, 28)) plt.show() """save model to drive""" model.save('my_cnn_mnist_model.h5')
..
CNN network Layout
Dataset
Run Tensorboard
>cd ./logs/scalars/20190730-105257
>tensorboard --logdir=./
almost 99% accuracy
Load Model and test one mnist image
...
"""load model from drive"""
from keras.models import load_model
new_model = load_model('my_cnn_mnist_model.h5')
"""load 1 image from drive"""
from PIL import Image
import numpy as np
"""test prediction"""img_path = './mnist_7_450.jpg'img = Image.open(img_path) #.convert("L") img = np.resize(img, (28,28,1)) im2arr = np.array(img) im2arr = im2arr.reshape(1,28,28,1) y_pred = new_model.predict_classes(im2arr) print(y_pred)
...
Test image
output
[7]
download minist jpeg file on here: http://study.marearts.com/2015/09/mnist-image-data-jpg-files.html
Labels:
CNN,
Deep learning,
Keras,
MNIST,
Tensorboard,
tensorflow,
Total
Subscribe to:
Posts (Atom)
-
Logistic Classifier The logistic classifier is similar to equation of the plane. W is weight vector, X is input vector and y is output...
-
I use MOG2 algorithm to background subtraction. The process is resize to small for more fast processing to blur for avoid noise affectio...
-
This is data acquisition source code of LMS511(SICK co.) Source code is made by MFC(vs 2008). The sensor is communicated by TCP/IP. ...
-
Background subtractor example souce code. OpenCV support about 3 types subtraction algorithm. Those are MOG, MOG2, GMG algorithms. Det...
-
Image size of origin is 320*240. Processing time is 30.96 second took. The result of stitching The resul...
-
Created Date : 2009.10. Language : C++ Tool : Visual Studio C++ 2008 Library & Utilized : Point Grey-FlyCapture, Triclops, OpenCV...
-
As you can see in the following video, I created a class that stitching n cameras in real time. https://www.youtube.com/user/feelmare/sear...
-
The MNIST dataset is a dataset of handwritten digits, comprising 60 000 training examples and 10 000 test examples. The dataset can be downl...
-
This post is about how to copy Mat data to vector and copy vector data to Mat. Reference this example source code. printf ( "/////...
-
This example source code is to extract HOG feature from images. And save descriptors to XML file. The source code explain how to use HOGD...