Price Prediction via Neural Network (Keras) - tensorflow

I am trying to do call price prediction my data set looks something like this
call_category_id,duration,Number 1,Number 2,price
9,24,77348,70000,0.01
9,144,77348,70000,0.08
9,138,77348,70000,0.08
9,12,77348,70000,0.01
The dialled number is split into two numbers(number 1, number 2) as i think it will improve prediction result. Usually first few digits of call dictate price per minute.
My model look something like this:
def get_model():
model = Sequential([
Dense(40,
activation='relu',
kernel_initializer='uniform',
input_shape=(4,)),
Dropout(0.3),
Dense(36,
activation='relu',
kernel_initializer='uniform'),
Dropout(0.3),
Dense(32,
activation='relu',
kernel_initializer='uniform'),
Dropout(0.3),
Dense(28,
activation='relu',
kernel_initializer='uniform'),
Dropout(0.3),
Dense(24,
activation='relu',
kernel_initializer='uniform'),
Dense(32,
activation='relu',
kernel_initializer='uniform'),
Dropout(0.3),
Dense(20,
activation='relu',
kernel_initializer='uniform'),
Dropout(0.3),
Dense(1, activation='linear'),
])
c_optimizers = optimizers.Adam()
model.compile(optimizer=c_optimizers,
loss='mean_squared_error',
metrics=['accuracy'])
return model
model.fit(
 x_train,
 y_train,
 batch_size=1024,
 epochs=1000,
 validation_data=(x_test, y_test),
 shuffle=True,
 callbacks=[tensor_board])
However the challenge is accuracy never improves it stuck at 19.6%.
39879/39879 [==============================] - 0s 5us/step - loss: 0.1646 - acc: 0.1969 - val_loss: 0.1003 - val_acc: 0.2065

Related

Training CNN Model and accuracy stays at 1

I've been trying to train this CNN Model, It's a Tensorflow tutorial and I just changed the dataset ( I used fruit 360 dataset) without altering the core of the code. When it finishes training the accuracy stays constant at 0.8565 it doesn't change and when I try and test some images it almost always wrong.
What am I doing wrong?
Code output after executing
Here's the code I used
[enter image description here][1]import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
import tarfile
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras import datasets, layers, models
from tensorflow import keras
import pathlib
dataset_url = "https://file.io/z5JM3sYAWXv4"
data_dir = tf.keras.utils.get_file(origin=dataset_url,
fname='tomatos',
untar=True,
extract=True)
data_dir = pathlib.Path(data_dir)
print(data_dir)
file_count = sum(len(files) for _, _, files in os.walk(r'tomatos'))
print(file_count)
batch_size = 32
img_height = 180
img_width = 180
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
print(class_names)
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
num_classes = len(class_names)
model = Sequential([
layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.summary()
epochs=2
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
data_augmentation = keras.Sequential(
[
layers.RandomFlip("horizontal",
input_shape=(img_height,
img_width,
3)),
layers.RandomRotation(0.1),
layers.RandomZoom(0.1),
]
)
model = Sequential([
data_augmentation,
layers.Rescaling(1./255),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Dropout(0.2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.summary()
epochs = 4
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
sunflower_url = "https://puffycarrot.com/wp-content/uploads/2017/04/Green-tomatoes.jpg"
sunflower_path = tf.keras.utils.get_file('tomato2', origin=sunflower_url)
img = tf.keras.utils.load_img(
sunflower_path, target_size=(img_height, img_width)
)
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
.format(class_names[np.argmax(score)], 100 * np.max(score))
)
#Yaman Tarawneh, I tried replicating your above mentioned code in Google colab (using TF 2.8) and in Pycharm (using TF 2.7) and did not find the error.
Please check the output image for Pycharm :
and got the same output in Google colab :
Total params: 3,988,898
Trainable params: 3,988,898
Non-trainable params: 0
_________________________________________________________________
Epoch 1/4
78/78 [==============================] - 8s 41ms/step - loss: 0.0309 - accuracy: 0.9835 - val_loss: 5.6374e-07 - val_accuracy: 1.0000
Epoch 2/4
78/78 [==============================] - 2s 25ms/step - loss: 5.7533e-07 - accuracy: 1.0000 - val_loss: 2.7360e-07 - val_accuracy: 1.0000
Epoch 3/4
78/78 [==============================] - 2s 25ms/step - loss: 3.0400e-07 - accuracy: 1.0000 - val_loss: 1.3978e-07 - val_accuracy: 1.0000
Epoch 4/4
78/78 [==============================] - 2s 25ms/step - loss: 1.7403e-07 - accuracy: 1.0000 - val_loss: 7.2102e-08 - val_accuracy: 1.0000
This image most likely belongs to Tomato not Ripened with a 100.00 percent confidence.
For further analysis if the issue still persists, Please let us know which Python and Tensorflow version are you using.

Tricks to improve CNN model performance

I am fitting a large CNN network on my training data, validating on 20%. It appears the model performs better on the training than the validation set. What do you suggest so I can improve the model performance.
CNN Architecture:
model = Sequential()
activ = 'relu'
model.add(Conv2D(32, (1, 3), strides=(1, 1), padding='same', activation=activ, input_shape=(1, 100, 4)))
model.add(Conv2D(32, (1, 3), strides=(1, 1), padding='same', activation=activ))
#model.add(BatchNormalization(axis = 3))
model.add(MaxPooling2D(pool_size=(1, 2) ))
model.add(Conv2D(64, (1, 3), strides=(1, 1), padding='same', activation=activ))
model.add(Conv2D(64, (1, 3), strides=(1, 1), padding='same', activation=activ))
model.add(MaxPooling2D(pool_size=(1, 2)))
model.add(Conv2D(128, (1, 3), strides=(1, 1), padding='same', activation=activ))
model.add(Conv2D(128, (1, 3), strides=(1, 1), padding='same', activation=activ ))
model.add(MaxPooling2D(pool_size=(1, 2)))
model.add(Dropout(.5))
model.add(Flatten())
A = model.output_shape
model.add(Dense(int(A[1] * 1/4.), activation=activ))
model.add(Dropout(.5))
model.add(Dense(5, activation='softmax'))
optimizer = Adam(lr=0.003, beta_1=0.9, beta_2=0.999, epsilon=1e-04, decay=0.0)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=100, batch_size=64, shuffle=False,
validation_split=0.2)
However, the validation accuracy doesn't change for the number of epochs.
Epoch 1/100
1065/1065 [==============================] - 14s 13ms/step - loss: 1.4174 - accuracy: 0.5945 - val_loss: 1.4966 - val_accuracy: 0.4417
Epoch 2/100
1065/1065 [==============================] - 14s 13ms/step - loss: 1.1494 - accuracy: 0.6207 - val_loss: 1.4634 - val_accuracy: 0.4417
Epoch 3/100
1065/1065 [==============================] - 19s 18ms/step - loss: 1.1111 - accuracy: 0.6196 - val_loss: 1.4674 - val_accuracy: 0.4417
Epoch 4/100
1065/1065 [==============================] - 15s 14ms/step - loss: 1.1040 - accuracy: 0.6196 - val_loss: 1.4660 - val_accuracy: 0.4417
Epoch 5/100
1065/1065 [==============================] - 18s 17ms/step - loss: 1.1027 - accuracy: 0.6196 - val_loss: 1.4624 - val_accuracy: 0.4417
NOTE: I Adam´s default learning rate 0.001 as well as 0.003 but the output is the same (log).
Your model is working but improving very slowly. I would reduce the dropout value down to .1 initially, then run the model and see if it overfits or not.If it does then slowly increase the dropout rate. Unless your data is already shuffled I would set shuffle=True in model.fit. Also you might try replacing the Flatten layer with a GlobalMaxPooling layer. I also recommend using the EarlyStopping callback which monitors validation and halts training if the loss fails to reduce after 'patience' number of consecutive epochs. Setting restore_best_weights=True will load the weights for the epoch with the lowest validation loss so you don't have to save then reload the weights. Set epochs to a large number to ensure this callback activates. Also use the ReduceLROnPlateau to automatically adjust the learning rate based on validation loss.
The code I use is shown below
es=tf.keras.callbacks.EarlyStopping( monitor="val_loss", patience=3,
verbose=1, restore_best_weights=True)
rlronp=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss", factor=0.5, patience=1,
verbose=1)
callbacks=[es, rlronp]
In model.fit set callbacks=callbacks. Increase the number of epochs you run to say 100 so that the early stopping callback triggers.

unable to train siamese with validation_data '<' not supported between instances of 'float' and 'list'

When i use keras model.fit such that i don't use "validation_data" and only x_train and y_train i don't get any error even if i use "validation_split" things work fine. Below is working piece of code
def siamese(x_train,y_train):
W_init = tf.keras.initializers.he_normal(seed=100)
b_init = tf.keras.initializers.he_normal(seed=50)
input_shape = (24,939)
left_input = Input(input_shape)
right_input = Input(input_shape)
encoder = Sequential()
encoder.add(Conv1D(filters=6,kernel_size=4, padding='same', activation='relu',input_shape=input_shape,kernel_initializer=W_init, bias_initializer=b_init))
encoder.add(BatchNormalization())
encoder.add(Dropout(.1))
encoder.add(MaxPool1D())
encoder.add(Conv1D(filters=4,kernel_size=3, padding='same', activation='relu'))
encoder.add(BatchNormalization())
encoder.add(Dropout(.1))
encoder.add(MaxPool1D())
encoder.add(Conv1D(filters=3,kernel_size=2, padding='same', activation='relu'))
encoder.add(BatchNormalization())
encoder.add(Dropout(.1))
encoder.add(MaxPool1D())
encoder.add(Flatten())
encoder.add(Dense(64,activation='relu'))
encoder.add(Dropout(.3))
encoded_l = encoder(left_input)
encoded_r = encoder(right_input)
distance = Lambda(euclidean_distance, output_shape=eucl_dist_output_shape)([encoded_l, encoded_r])
adam = optimizers.Adam(lr=.001)
earlyStopping = EarlyStopping(monitor='loss',min_delta=0,patience=3,verbose=1,restore_best_weights=False)
callback_early_stop_reduceLROnPlateau=[earlyStopping]
model = Model([left_input, right_input], distance)
model.compile(loss=contrastive_loss, optimizer=adam,metrics=[accuracy])
model.summary()
history = model.fit([(x_train[:,:,:,0]).astype(np.float32),(x_train[:,:,:,1]).astype(np.float32)],y_train,validation_split = .15,batch_size=64,epochs=4,callbacks=callback_early_stop_reduceLROnPlateau)
return model,history
model1,history1=siamese(xtrain_np_img1_img2,y_train_numpy)
Output::::
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_5 (InputLayer) (None, 24, 939) 0
__________________________________________________________________________________________________
input_6 (InputLayer) (None, 24, 939) 0
__________________________________________________________________________________________________
sequential_3 (Sequential) (None, 64) 23337 input_5[0][0]
input_6[0][0]
__________________________________________________________________________________________________
lambda_3 (Lambda) (None, 1) 0 sequential_3[1][0]
sequential_3[2][0]
==================================================================================================
Total params: 23,337
Trainable params: 23,311
Non-trainable params: 26
__________________________________________________________________________________________________
Train on 12653 samples, validate on 2233 samples
Epoch 1/4
12653/12653 [==============================] - 8s 668us/step - loss: 5.2016 - accuracy: 0.4152 - val_loss: 0.1739 - val_accuracy: 0.7323
Epoch 2/4
12653/12653 [==============================] - 7s 533us/step - loss: nan - accuracy: 0.4359 - val_loss: nan - val_accuracy: 1.0000
Epoch 3/4
12653/12653 [==============================] - 7s 539us/step - loss: nan - accuracy: 0.4117 - val_loss: nan - val_accuracy: 1.0000
Epoch 4/4
12653/12653 [==============================] - 7s 532us/step - loss: nan - accuracy: 0.4117 - val_loss: nan - val_accuracy: 1.0000
Epoch 00004: early stopping
Now i wanted to introduce "validation_data" and not use "validation_split"
So i tried first
def siamese(x_train,y_train,x_val,y_val):
W_init = tf.keras.initializers.he_normal(seed=100)
b_init = tf.keras.initializers.he_normal(seed=50)
input_shape = (24,939)
left_input = Input(input_shape)
right_input = Input(input_shape)
encoder = Sequential()
encoder.add(Conv1D(filters=6,kernel_size=4, padding='same', activation='relu',input_shape=input_shape,kernel_initializer=W_init, bias_initializer=b_init))
encoder.add(BatchNormalization())
encoder.add(Dropout(.1))
encoder.add(MaxPool1D())
encoder.add(Conv1D(filters=4,kernel_size=3, padding='same', activation='relu'))
encoder.add(BatchNormalization())
encoder.add(Dropout(.1))
encoder.add(MaxPool1D())
encoder.add(Conv1D(filters=3,kernel_size=2, padding='same', activation='relu'))
encoder.add(BatchNormalization())
encoder.add(Dropout(.1))
encoder.add(MaxPool1D())
encoder.add(Flatten())
encoder.add(Dense(64,activation='relu'))
encoder.add(Dropout(.3))
encoded_l = encoder(left_input)
encoded_r = encoder(right_input)
distance = Lambda(euclidean_distance, output_shape=eucl_dist_output_shape)([encoded_l, encoded_r])
adam = optimizers.Adam(lr=.001)
earlyStopping = EarlyStopping(monitor='loss',min_delta=0,patience=3,verbose=1,restore_best_weights=False)
callback_early_stop_reduceLROnPlateau=[earlyStopping]
model = Model([left_input, right_input], distance)
model.compile(loss=contrastive_loss, optimizer=adam,metrics=[accuracy])
model.summary()
history = model.fit([(x_train[:,:,:,0]).astype(np.float32),(x_train[:,:,:,1]).astype(np.float32)],y_train,tuple([(x_val[:,:,:,0]).astype(np.float32),(x_val[:,:,:,1]).astype(np.float32)]),y_val,batch_size=128,epochs=4,callbacks=callback_early_stop_reduceLROnPlateau)
return model,history
model1,history1=siamese(xtrain_np_img1_img2,y_train_numpy,xtest_np_img1_img2,y_test_numpy)
The error i got is
TypeError: fit() got multiple values for argument 'batch_size'
So i tried another way since i was not able to troubleshoot above issue as
def siamese(x_train,y_train,x_val,y_val,batch_size,epochs,callbacks):
W_init = tf.keras.initializers.he_normal(seed=100)
b_init = tf.keras.initializers.he_normal(seed=50)
input_shape = (24,939)
left_input = Input(input_shape)
right_input = Input(input_shape)
encoder = Sequential()
encoder.add(Conv1D(filters=6,kernel_size=4, padding='same', activation='relu',input_shape=input_shape,kernel_initializer=W_init, bias_initializer=b_init))
encoder.add(BatchNormalization())
encoder.add(Dropout(.1))
encoder.add(MaxPool1D())
encoder.add(Conv1D(filters=4,kernel_size=3, padding='same', activation='relu'))
encoder.add(BatchNormalization())
encoder.add(Dropout(.1))
encoder.add(MaxPool1D())
encoder.add(Conv1D(filters=3,kernel_size=2, padding='same', activation='relu'))
encoder.add(BatchNormalization())
encoder.add(Dropout(.1))
encoder.add(MaxPool1D())
encoder.add(Flatten())
encoder.add(Dense(64,activation='relu'))
encoder.add(Dropout(.3))
encoded_l = encoder(left_input)
encoded_r = encoder(right_input)
distance = Lambda(euclidean_distance, output_shape=eucl_dist_output_shape)([encoded_l, encoded_r])
adam = optimizers.Adam(lr=.001)
earlyStopping = EarlyStopping(monitor='loss',min_delta=0,patience=3,verbose=1,restore_best_weights=False)
callback_early_stop_reduceLROnPlateau=[earlyStopping]
model = Model([left_input, right_input], distance)
model.compile(loss=contrastive_loss, optimizer=adam,metrics=[accuracy])
model.summary()
history = model.fit([(x_train[:,:,:,0]).astype(np.float32),(x_train[:,:,:,1]).astype(np.float32)],y_train,tuple([(x_val[:,:,:,0]).astype(np.float32),(x_val[:,:,:,1]).astype(np.float32)]),y_val,batch_size,epochs,callbacks)
return model,history
model1,history1=siamese(xtrain_np_img1_img2,y_train_numpy,xtest_np_img1_img2,y_test_numpy,64,4,callback_early_stop_reduceLROnPlateau)
Now this time error is
TypeError Traceback (most recent call last)
<ipython-input-17-fd746aea477d> in <module>
----> 1 model1,history1=siamese(xtrain_np_img1_img2,y_train_numpy,xtest_np_img1_img2,y_test_numpy,64,4,callback_early_stop_reduceLROnPlateau)
<ipython-input-15-cebaa8a123ad> in siamese(x_train, y_train, x_val, y_val, batch_size, epochs, callbacks)
36 model.summary()
---> 38 history = model.fit([(x_train[:,:,:,0]).astype(np.float32),(x_train[:,:,:,1]).astype(np.float32)],y_train,tuple([(x_val[:,:,:,0]).astype(np.float32),(x_val[:,:,:,1]).astype(np.float32)]),y_val,batch_size,epochs,callbacks)
39 return model,history
~\AppData\Roaming\Python\Python37\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
1179 val_inputs = val_x + val_y + val_sample_weights
1180
-> 1181 elif validation_split and 0. < validation_split < 1.:
1182 if any(K.is_tensor(t) for t in x):
1183 raise ValueError(
TypeError: '<' not supported between instances of 'float' and 'list'
I am pretty sure i am making some trivial mistake as i am learning machine learning.
The reason why i am trying this because i want to use a tool named "talos" and since i am working with siamese network which takes multiple input and for talos to work properly i can't use validation_split but validation_data
https://autonomio.github.io/talos/#/Examples_Multiple_Inputs
The reason why i want to use talos is for query for another thread because my model is not performing well so i thought may be i should first try hyperparameter tuning.

ValueError: Input 0 of layer sequential_22 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [28, 28, 1]

from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
from keras.datasets import mnist
from keras.utils import to_categorical
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)
I am getting an error:
ValueError: Input 0 of layer sequential_22 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [28, 28, 1]
I tried using the Keras 2.8, fashion_mnist dataset and it is working.
import keras
print(keras.__version__)
from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
from keras.datasets import fashion_mnist
from tensorflow.keras.utils import to_categorical
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)
Output:
2.8.0
Epoch 1/5
938/938 [==============================] - 53s 55ms/step - loss: 0.5523 - accuracy: 0.7965
Epoch 2/5
938/938 [==============================] - 51s 55ms/step - loss: 0.3333 - accuracy: 0.8780
Epoch 3/5
938/938 [==============================] - 54s 58ms/step - loss: 0.2832 - accuracy: 0.8968
Epoch 4/5
938/938 [==============================] - 52s 55ms/step - loss: 0.2508 - accuracy: 0.9079
Epoch 5/5
938/938 [==============================] - 51s 55ms/step - loss: 0.2264 - accuracy: 0.9168
<keras.callbacks.History at 0x7f1c1b0a5390>

Regressing on an image to predict a scalar

Given 256x256 rgb input images, I'm trying to regress to predict a point on the X axis of the image (0-48000)
Initially, I tried [mobile_net -> GlobalAveragePooling2D -> several Dense layers]. I didn't realize Pooling was discarding the spatial information.
Last night, I trained on a simpler net, the loss decreased all night, but it's predicting negative values.
How can I modify this architecture to predict a 0-48000 scalar?
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(64, kernel_size=3, activation='relu', input_shape=(256,256,3)),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1, kernel_initializer='normal'),
])
model.compile(loss='mse', optimizer='adam', metrics=['mse', 'mae', 'mape']) #
EDIT:
Inferring from my netwrok, I'm getting vastly different outputs, each run, for the SAME file. How is that possible?
Infer outputs, running multiple times on the same file:
-312864.9444580078
762.7029418945312
193352.7603149414
Here is the inference fn:
def infer(checkpoint_path):
png_file = ['3023_28338_26_m.png', '3023_28338_26_m.png'][1]
test_file = data_root + png_file
onset = png_file.strip('_m.png.').split('_')[1]
img = load_and_preprocess_from_path_label(test_file, 0)
tst = np.expand_dims(img[0], axis=0)
model = load_model_and_checkpoint(checkpoint_path)
val = model.predict(tst)[0][0] * 48000
Here is the final epoch of training.
2019-05-26 11:11:56.698907: I tensorflow/core/kernels/data/shuffle_dataset_op.cc:150] Shuffle buffer filled.
94/95 [============================>.] - ETA: 0s - loss: 0.0063 - mse: 0.0063 - mae: 0.0627 - mape: 93.2817
Epoch 00100: saving model to /media/caseybasichis/sp_data/sp_data/datasets/one_sec_onset_01/model7.ckpt
95/95 [==============================] - 47s 500ms/step - loss: 0.0063 - mse: 0.0063 - mae: 0.0626 - mape: 93.2076
Here is the latest network.
mobile_net = tf.keras.applications.ResNet50(input_shape=(256, 256, 3), include_top=False, weights='imagenet')
mobile_net.trainable=False
model = tf.keras.Sequential([
mobile_net,
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, kernel_initializer='normal', activation='relu'),
tf.keras.layers.BatchNormalization(axis=chanDim),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1, kernel_initializer='normal', activation='linear'), # activation='sigmoid'
])
model.compile(loss='mse', optimizer='adam', metrics=['mse', 'mae', 'mape']) # mean_squared_logarithmic_error
You can simply use Sigmoid activation on the last layer and multiply the output by the scale (in a Lambda layer or preferably just scale the output out side the network)
model.add(Activation('sigmoid'))
model.add(Lambda(lambda x: 48000*x))
or
model.add(Activation('sigmoid'))
...
model.fit(x_train, y_train/48000.0)