Last active
February 1, 2023 18:12
-
-
Save yohanesgultom/78467788a5421a318c860a3b0f85b5a4 to your computer and use it in GitHub Desktop.
Python machine learning scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python machine learning scripts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rainfall time series prediction usint LSTM and Dropout | |
# Base on: | |
# http://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/ | |
# http://machinelearningmastery.com/dropout-regularization-deep-learning-models-keras/ | |
import numpy | |
import matplotlib.pyplot as plt | |
import pandas | |
import math | |
from keras.models import Sequential | |
from keras.layers import Dense, LSTM, Dropout | |
from sklearn.preprocessing import MinMaxScaler | |
from sklearn.metrics import mean_squared_error | |
# convert an array of values into a dataset matrix | |
def create_dataset(dataset, look_back=1): | |
dataX, dataY = [], [] | |
for i in range(len(dataset) - look_back - 1): | |
a = dataset[i:(i + look_back), 0] | |
dataX.append(a) | |
dataY.append(dataset[i + look_back, 0]) | |
return numpy.array(dataX), numpy.array(dataY) | |
# load the dataset of rainfall per month from salidas.csv | |
# data samples: | |
# 73.2 | |
# 60.3 | |
# 32.0 | |
# 41.9 | |
dataframe = pandas.read_csv('salidas.csv', usecols=[0], engine='python') | |
dataset = dataframe.values | |
dataset = dataset.astype('float32') | |
# normalize the dataset | |
scaler = MinMaxScaler(feature_range=(0, 1)) | |
dataset = scaler.fit_transform(dataset) | |
# split into train and test sets | |
train_size = int(len(dataset) * 0.67) | |
test_size = len(dataset) - train_size | |
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :] | |
# reshape into X=t and Y=t+1 | |
look_back = 12 | |
trainX, trainY = create_dataset(train, look_back) | |
testX, testY = create_dataset(test, look_back) | |
# reshape input to be [samples, time steps, features] | |
trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) | |
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1])) | |
# create and fit the LSTM network | |
model = Sequential() | |
model.add(LSTM(4, input_dim=look_back)) | |
model.add(Dropout(0.2)) | |
model.add(Dense(1)) | |
model.compile(loss='mean_squared_error', optimizer='adam') | |
history = model.fit(trainX, trainY, validation_split=0.33, nb_epoch=100, batch_size=1) | |
# summarize history for loss | |
plt.plot(history.history['loss']) | |
plt.plot(history.history['val_loss']) | |
plt.title('model loss') | |
plt.ylabel('loss') | |
plt.xlabel('epoch') | |
plt.legend(['train', 'validation'], loc='upper left') | |
plt.show() | |
# # make predictions | |
# trainPredict = model.predict(trainX) | |
# testPredict = model.predict(testX) | |
# | |
# # invert predictions | |
# trainPredict = scaler.inverse_transform(trainPredict) | |
# trainY = scaler.inverse_transform([trainY]) | |
# testPredict = scaler.inverse_transform(testPredict) | |
# testY = scaler.inverse_transform([testY]) | |
# | |
# # calculate root mean squared error | |
# trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:, 0])) | |
# print('Train Score: %.2f RMSE' % (trainScore)) | |
# testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:, 0])) | |
# print('Test Score: %.2f RMSE' % (testScore)) | |
# | |
# # shift train predictions for plotting | |
# trainPredictPlot = numpy.empty_like(dataset) | |
# trainPredictPlot[:, :] = numpy.nan | |
# trainPredictPlot[look_back:len(trainPredict) + look_back, :] = trainPredict | |
# | |
# # shift test predictions for plotting | |
# testPredictPlot = numpy.empty_like(dataset) | |
# testPredictPlot[:, :] = numpy.nan | |
# testPredictPlot[len(trainPredict) + (look_back * 2) + 1:len(dataset) - 1, :] = testPredict | |
# | |
# # plot baseline and predictions | |
# plt.plot(scaler.inverse_transform(dataset)) | |
# plt.plot(trainPredictPlot) | |
# plt.plot(testPredictPlot) | |
# plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Sequential | |
from keras.layers.core import Flatten, Dense, Dropout | |
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D | |
from keras.optimizers import SGD | |
import cv2, numpy as np | |
import linecache | |
import sys | |
import h5py | |
def VGG_16(weights_path=None): | |
model = Sequential() | |
model.add(ZeroPadding2D((1,1),input_shape=(3,300,300))) | |
#model.add(ZeroPadding2D((1,1),input_shape=(3,224,224))) | |
model.add(Convolution2D(64, 3, 3, activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(64, 3, 3, activation='relu')) | |
model.add(MaxPooling2D((2,2), strides=(2,2))) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(128, 3, 3, activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(128, 3, 3, activation='relu')) | |
model.add(MaxPooling2D((2,2), strides=(2,2))) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(256, 3, 3, activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(256, 3, 3, activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(256, 3, 3, activation='relu')) | |
model.add(MaxPooling2D((2,2), strides=(2,2))) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, 3, 3, activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, 3, 3, activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, 3, 3, activation='relu')) | |
model.add(MaxPooling2D((2,2), strides=(2,2))) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, 3, 3, activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, 3, 3, activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, 3, 3, activation='relu')) | |
model.add(MaxPooling2D((2,2), strides=(2,2))) | |
# load weights | |
if weights_path: | |
f = h5py.File(weights_path) | |
for k in range(f.attrs['nb_layers']): | |
if k >= len(model.layers) - 1: | |
# we don't look at the last two layers in the savefile (fully-connected and activation) | |
break | |
g = f['layer_{}'.format(k)] | |
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])] | |
layer = model.layers[k] | |
#if layer.__class__.__name__ in ['Convolution1D', 'Convolution2D', 'Convolution3D', 'AtrousConvolution2D']: | |
# weights[0] = np.transpose(weights[0], (2, 3, 1, 0)) | |
layer.set_weights(weights) | |
# freeze | |
layer.trainable = False | |
f.close() | |
model.add(Flatten()) | |
model.add(Dense(4096, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(4096, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(1000, activation='softmax')) | |
return model | |
def print_weights(model): | |
for layer in model.layers: | |
print(layer) | |
print(layer.get_weights()) | |
if __name__ == "__main__": | |
img_filename = 'husky.jpg' | |
img_label_index = 250 | |
#im = cv2.resize(cv2.imread(img_filename), (224, 224)).astype(np.float32) | |
im = cv2.resize(cv2.imread(img_filename), (300, 300)).astype(np.float32) | |
im[:,:,0] -= 103.939 | |
im[:,:,1] -= 116.779 | |
im[:,:,2] -= 123.68 | |
im = im.transpose((2,0,1)) | |
X = np.expand_dims(im, axis=0) | |
Y = np.zeros(1000) | |
Y[img_label_index] = 1 | |
Y = np.expand_dims(Y, axis=0) | |
print(X.shape) | |
print(Y.shape) | |
# load custom vgg16 and train 1 epoch | |
model = VGG_16('vgg16_weights.h5') | |
#print_weights(model) | |
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) | |
model.compile(optimizer=sgd, loss='categorical_crossentropy') | |
model.fit(X, Y, batch_size=1, nb_epoch=1, validation_data=(X, Y)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Update of example of Keras VGG16 custom input shape | |
Using: | |
Keras==2.2.4 | |
tensorflow-gpu==1.13.1 | |
''' | |
import h5py, pickle | |
import numpy as np | |
from keras.models import Model | |
from keras.layers import Dense, GlobalAveragePooling2D, Dropout | |
from keras.applications.vgg16 import VGG16 | |
from keras.utils import to_categorical | |
vgg16_weights = '/home/yohanesgultom/Downloads/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5' | |
dir_path = '/home/yohanesgultom/Downloads/cifar-10-python/cifar-10-batches-py' | |
def load_cfar10_batch(cifar10_dataset_folder_path, batch_id): | |
with open(cifar10_dataset_folder_path + '/data_batch_' + str(batch_id), mode='rb') as file: | |
# note the encoding type is 'latin1' | |
batch = pickle.load(file, encoding='latin1') | |
features = batch['data'].reshape((len(batch['data']), 3, 32, 32)).transpose(0, 2, 3, 1) | |
labels = batch['labels'] | |
return np.array(features), to_categorical(labels) | |
X, y = load_cfar10_batch(dir_path, 1) | |
base_model = VGG16(include_top=False, weights=vgg16_weights, input_shape=(32, 32, 3)) | |
# add a global spatial average pooling layer | |
# fully-connected layer and prediction layer | |
x = base_model.output | |
x = GlobalAveragePooling2D()(x) | |
x = Dense(512, activation='relu')(x) | |
x = Dropout(0.3)(x) | |
predictions = Dense(10, activation='softmax')(x) | |
# freeze vgg16 layers | |
for layer in base_model.layers: | |
layer.trainable = False | |
model = Model(inputs=base_model.input, outputs=predictions) | |
model.summary() | |
model.compile(optimizer='rmsprop', loss='categorical_crossentropy') | |
model.fit(X, y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment