denoising 1d timeseries using deep learning - tensorflow

I want to train a 1d convolutional model in the unsupervised way for denoising the 1d timeseries data.In this regard I generated the 1d noisy timeseries as mentioned in inpdata and i want to denoise it.
My code is
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from keras.models import Sequential
from keras.layers import Dense,Input,Reshape
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D,AveragePooling1D,UpSampling1D
from tensorflow.keras.models import Sequential,Model
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv1D, Dense, MaxPooling1D, Flatten
from tensorflow.keras.optimizers import Adam
inpdata=np.random.rand(128,)
print(inpdata.shape[0])
print(inpdata)
model = Sequential()
model.add(Conv1D(64, 3, activation='relu', input_shape=(128,1)))
model.add(MaxPooling1D(2))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(2))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(28, activation='softmax'))
model.compile(Adam(lr=.0001), loss='mse', metrics=['accuracy'])
model.fit(inpdata,inpdata, batch_size=32, epochs=3, validation_split=0.1)
model.summary()
But here i am getting error is ValueError: Error when checking input: expected conv1d_input to have 3 dimensions, but got array with shape (128, 1)
I cannot understand where I am doing mistake and unable to denoise.Hope experts may suggest a better solution with an example.Thanks.

Related

Keras attribute error due to tensorflow version support in CNN

I'm working on a convolutional neural network and I get the error
AttributeError: module 'keras.utils.generic_utils' has no attribute
'populate_dict_with_module_objects
here is the code excluding the data processing on the dataset.
my version for Keras is 2.4.3, TensorFlow is 2.5.0 and python is 3.8
I've seen many other people facing similar issues but I believe it has to do with compatibility versions of TensorFlow. I have tried like 3-4 versions of TensorFlow but they all gave me obscure errors like the one above.
I hope this can be fixed because otherwise, I don't know how I will continue with the network
import pickle
import time
import os
import cv2
import random
import matplotlib.pyplot as plt
import numpy as np
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Activation
model = Sequential()
model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Flatten())
model.add(Dense(128, input_shape = X.shape[1:]), activation = 'relu')
model.add(Dense(4,activation = 'softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics = ['accuracy'])
model.fit(X,Y, epochs =2, validation_split = 0.2)
If you're using tensorflow 2.x, you should not mix standalone keras and new tf.keras in the same import. You above code should have imported as follows: (test on version 2.4, 2.5 in colab).
import pickle, time, os ,cv2, random
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (Conv2D, MaxPooling2D, Dense,
Flatten, Activation)
model = Sequential()
model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dense(4,activation = 'softmax'))
Also, note that input_shape at the middle of the model doesn't have any effect (as you do in Dense layer).

Error in Python: Function call stack: train_function

I am running a snippet of code that is supposed to train a manuscript recognition model, in Python language in a COLAB work environment.
The code loads a database of pictures of literature written by people from MNIST and practices them.
The code:
import numpy
from keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras import utils as np_utils
from tensorflow.keras import backend as K
K.set_image_data_format('channels_first')
from matplotlib import pyplot as plt
#load data from mnist
(xTrain,yTrain),(xTest,yTest)=mnist.load_data()
#reshape the images to be 28*28 pixels
xTrain=xTrain.reshape(xTrain.shape[0],1,28,28).astype('float32')
xTest=xTest.reshape(xTest.shape[0],1,28,28).astype('float32')
#normalize inputs from 0-255 to 0-1
xTrain=xTrain/255
xTest=xTest/255
#one hot encode outputs
yTrain=np_utils.to_categorical(yTrain)
yTest=np_utils.to_categorical(yTest)
num_classes=yTest.shape[1]
def baseline_model():
#create model
model=Sequential()
model.add(Conv2D(32,(5,5), input_shape=(1,28,28),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
#complie model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model=baseline_model()
model.fit(xTrain, yTrain, validation_data=(xTest, yTest), epochs=1, batch_size=200, verbose=2)
The problem is that the code returns an error in the last line.
The error:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-111-ec22b5dcc4e3> in <module>()
63
64 model=baseline_model()
---> 65 model.fit(xTrain, yTrain, validation_data=(xTest, yTest), epochs=1, batch_size=200, verbose=2)
6 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
InvalidArgumentError: Default MaxPoolingOp only supports NHWC on device type CPU
[[node sequential_36/max_pooling2d_21/MaxPool (defined at <ipython-input-111-ec22b5dcc4e3>:65) ]] [Op:__inference_train_function_10696]
Function call stack:
train_function
Would appreciate help,
Thank you
InvalidArgumentError: Default MaxPoolingOp only supports NHWC on
device type CPU
As the error clearly says, it can't work with NCHW (i.e. batch_size, channels, height, width) on CPU.
If you want to use CPU, then you should change the format as NHWC (i.e.batch_size, height, width, channels) then input should be (28,28,1).
One more suggestion never mix imports from tensorflow and keras.
Where as on GPU, it supports with NCHW. You can refer working code as shown below
import numpy
from tensorflow.keras.datasets import mnist # import from TF
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras import utils as np_utils
from tensorflow.keras import backend as K
K.set_image_data_format('channels_first')
from matplotlib import pyplot as plt
#load data from mnist
(xTrain,yTrain),(xTest,yTest)=mnist.load_data()
#reshape the images to be 28*28 pixels
xTrain=xTrain.reshape(xTrain.shape[0],1,28,28).astype('float32')
xTest=xTest.reshape(xTest.shape[0],1,28,28).astype('float32')
#normalize inputs from 0-255 to 0-1
xTrain=xTrain/255
xTest=xTest/255
#one hot encode outputs
yTrain=np_utils.to_categorical(yTrain)
yTest=np_utils.to_categorical(yTest)
num_classes=yTest.shape[1]
def baseline_model():
#create model
model=Sequential()
model.add(Conv2D(32,(5,5), input_shape=(1,28,28),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
#complie model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model=baseline_model()
model.fit(xTrain, yTrain, validation_data=(xTest, yTest), epochs=1, batch_size=200, verbose=2)
output:
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
300/300 - 34s - loss: 0.2372 - accuracy: 0.9304 - val_loss: 0.0756 - val_accuracy: 0.9760

Wrong CIFAR-10 data format in convolution neural network

i have imported data as following
from keras.datasets import cifar10
import matplotlib.pyplot as plt
(X_train,y_train),(X_test,y_test) =cifar10.load_data()
for i in range(9):
plt.subplot(330+1+i)
plt.imshow(X_train[i])
plt.show()
it works fine as it is given in result :
next i have defined following convolution neural network structure
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.constraints import max_norm
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.layers import Convolution2D
from tensorflow.keras.layers import MaxPooling2D
from keras.utils import np_utils
model =Sequential()
model.add(Convolution2D(filters=32,kernel_size=3,input_shape=(3,32,32),padding='same',activation='relu',data_format='channels_first',kernel_constraint=max_norm(3)))
model.add(Dropout(0.2))
model.add(Convolution2D(filters=32,kernel_size=3,activation='relu',padding='same',kernel_constraint=max_norm(3)))
model.add(MaxPooling2D(pool_size=2))
model.add(Flatten())
model.add(Dense(units=512,activation='relu',kernel_constraint=max_norm(3)))
model.add(Dropout(0.5))
model.add(Dense(num_classes,activation='softmax'))
i have done all necessary compile procedures
Epochs=25
lrate =0.01
decay =lrate /epochs
sgd =SGD(learning_rate=decay,momentum=0.9,nesterov=False)
model.compile(loss='categorical_crossentropy' , optimizer='sgd', metrics=['accuracy' ])
print(model.summary())
and result is here:
after runing following code
model.fit(X_train, y_train, validation_data=(X_test, y_test),epochs=Epochs,batch_size=32, verbose=2)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
it gives me error :
ValueError: Input 0 of layer sequential_7 is incompatible with the layer: expected axis 1 of input shape to have value 3 but received input with shape [None, 32, 32, 3]
please help me to fix this error
The input shape in your first convolutional layer should be input_shape=(32,32,3) instead of input_shape=(3,32,32). You should also set the data_format to the default "channels_last". The reason for setting it up like this is that the shape of cifar10 images is (32,32,3) and not (3,32,32)
you should always be careful about the input shape of the first layer of CNN using Conv2D. In tensorflow the input shape parameters are given as (batch_size, img_h, img_w, channels).
In contrary to pytorch it is (batch_size, channels, img_h, img_w). So, it should be (32,32,3) not (3, 32, 32).

Any ideas how to slove problem with activation function?

I have a problem with loading self pretrained keras model.
When i make predictions directly after training model everything works fine.
My code:
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
import matplotlib.pyplot as plt
import numpy as np
import os
from keras.models import load_model
"""
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
from tensorflow.keras.models import load_model
"""
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# Reshaping the array to 4-dims so that it can work with the Keras API
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# Making sure that the values are float so that we can get decimal points after division
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# Normalizing the RGB codes by dividing it to the max RGB value.
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print('Number of images in x_train', x_train.shape[0])
print('Number of images in x_test', x_test.shape[0])
# Importing the required Keras modules containing model and layers
def train():
# Creating a Sequential Model and adding the layers
model = Sequential()
model.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # Flattening the 2D arrays for fully connected layers
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dropout(0.2))
model.add(Dense(10, activation=tf.nn.softmax))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x=x_train,y=y_train, epochs=10)
print(model.summary())
model.save('x.h5')
def eval():
model = load_model('x.h5')
scores = model.evaluate(x_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
train()
eval()
And i got an error:
ValueError: Unknown activation function:softmax_v2
I've tried to use different tensorflow versions (1.15, 2.0, 1.5) but this changes nothing.
Any ideas what is wrong with it?
Edit:
This problem occurs only when i try to load model.

How to Fit my model using transfer learning Vgg

I'm new to Machine learning i got 1 example of Cat vs Dog image classification
and here is the link to it
https://pythonprogramming.net/convolutional-neural-network-kats-vs-dogs-machine-learning-tutorial/
It worked perfectly , but now when i want to implement transfer learning to it using VGG16 , it's not working
from keras.models import Sequential, Model, load_model
from keras.applications.vgg16 import VGG16
from keras import optimizers
from keras.layers import Dropout, Flatten, Dense, Activation
from keras.models import Sequential
from keras import utils
train = train_data[:-500]
test = train_data[-500:]
X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
Y = [i[1] for i in train]
test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
test_y = np.array([i[1] for i in test])
from keras.layers import Activation, Conv2D, Dense, Dropout, Flatten, MaxPooling2D
from keras.models import Sequential
modelvgg = VGG16(weights='imagenet', include_top=False, input_shape=(50,50,3))
type(modelvgg)
modelvgg.layers.pop()
model = Sequential()
for layer in modelvgg.layers:
model.add(layer)
for layer in model.layers:
layer.trainable = False
model.add(Dense(1, activation= 'sigmoid'))
model.compile(optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')
model.summary()
model.fit({'input': X}, {'targets': Y}, n_epoch=10, validation_set=({'input': test_x}, {'targets': test_y}),
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
Here is the Error i always get
I guess there's a problem i guess of how i'm fitting my module so please i need help with that
Unrecognized keyword arguments: {'n_epoch': 10, 'validation_set': ({'input': array([[[[ 41, 40, 36],
[ 43, 42, 38],
[ 43, 42, 38],
The tutorial you linked does not use keras but tflearn, no wonder the fit call does not work. A correct call with keras would be:
model.fit(X, Y, epochs=10, validation_data=(test_x, test_y))