I am working on an images classification using Keras.
There is my model:
model = Sequential()
model.add(Conv2D(filters = 8, kernel_size = (3,3),padding = 'Same',
activation ='relu', input_shape = (64,64,3)))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(filters = 16, kernel_size = (3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(filters = 32, kernel_size = (3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(3, activation = "sigmoid"))
I would like to avoid Flatten() since in this case we are losing some spatial information. I looked some tutorials, but all of them used Flatten(). Is it possible to use some thing like deconvolution instead?
Flatten is fine.
The "spatial" relations of Flatten going into a Dense layer are, in a sense preserved. As all values from a particular position map to the same weight in the dense layer. So every point is mapped consistently across the dataset. The "spatial" relations mapped in the convolutional layers are looking for localized patterns, and in those layers keeping the input unaltered is important.
Related
model.fit(X_train, y_train, batch_size = 16, initial_epoch=10,epochs=15, verbose = 1, validation_data = (X_test, y_test))
I have trained a model using LeNet architecture for some symbol images. I have to detect these symbols using sliding window cnn algorithm on different img. How can I do that?
Can anyone help
You can use model.predict() function on the test dataset for prediction.
Like this:
predictions = model.predict(X_test)
I am using consistency between two predicted segmentation maps on unlabeled data. For labeled data, I’m using nn.BCEwithLogitsLoss and Dice Loss.
I’m working on videos that’s why 5 dimensions output.
(batch_size, channels, frames, height, width)
I want to know how can we compare two predicted segmentation maps.
gmentation maps.
# gt_seg - Ground truth segmentation map. - (8, 1, 8, 112, 112)
# aug_gt_seg - Augmented ground truth segmentation map - (8, 1, 8, 112, 112)
predicted_seg_1 = model(data, targets) # (8, 1, 8, 112, 112)
predicted_seg_2 = model(augmented_data, augmented_targets) #(8, 1, 8, 112, 112)
# define criterion
seg_criterion_1 = nn.BCEwithLogitsLoss(size_average=True)
seg_criterion_2 = nn.DiceLoss()
# labeled losses
supervised_loss_1 = seg_criterion_1(predicted_seg_1, gt_seg)
supervised_loss_2 = seg_criterion_2(predicted_seg_1, gt_seg)
# Consistency loss
if consistency_loss == "l2":
consistency_criterion = nn.MSELoss()
cons_loss = consistency_criterion(predicted_gt_seg_1, predicted_gt_seg_2)
elif consistency_loss == "l1":
consistency_criterion = nn.L1Loss()
cons_loss = consistency_criterion(predicted_gt_seg_1, predicted_gt_seg_2)
total_supervised_loss = supervised_loss_1 + supervised_loss_2
total_consistency_loss = cons_loss
Is this the right way to apply consistency between two predicted segmentation maps?
I’m mainly confused due to the definition on the torch website. It’s a comparison with input x with target y. I thought it looks correct since I want both predicted segmentation maps similar. But, 2nd segmentation map is not a target. That’s why I’m confused. Because if this could be valid, then every loss function can be applied in some or another way. That doesn’t look appealing to me. If it’s the correct way to compare, can it be extended to other segmentation-based losses such as Dice Loss, IoU Loss, etc.?
One more query regarding loss computation on labeled data:
# gt_seg - Ground truth segmentation map
# aug_gt_seg - Augmented ground truth segmentation map
predicted_seg_1 = model(data, targets)
predicted_seg_2 = model(augmented_data, augmented_targets)
# define criterion
seg_criterion_1 = nn.BCEwithLogitsLoss(size_average=True)
seg_criterion_2 = nn.DiceLoss()
# labeled losses
supervised_loss_1 = seg_criterion_1(predicted_seg_1, gt_seg)
supervised_loss_2 = seg_criterion_2(predicted_seg_1, gt_seg)
# augmented labeled losses
aug_supervised_loss_1 = seg_criterion_1(predicted_seg_2, aug_gt_seg)
aug_supervised_loss_2 = seg_criterion_2(predicted_seg_2, aug_gt_seg)
total_supervised_loss = supervised_loss_1 + supervised_loss_2 + aug_supervised_loss_1 + aug_supervised_loss_2
Is the calculation of total_supervised_loss correct? Can I apply loss.backward() on this?
Yes, this is a valid way to implement consistency loss. The nomenclature used by pytorch documentation lists one input as the target and the other as the prediction, but consider that L1, L2, Dice, and IOU loss are all symmetrical (that is, Loss(a,b) = Loss(b,a)). So any of these functions will accomplish a form of consistency loss with no regard for whether one input is actually a ground-truth or "target".
I am trying to build a LSTM Autoendoer for anomaly detection.
But the model seems not work for my data.
Here is the normal data that I use it for training.
And here is abnormal data that I use it for validation.
If model works, its loss should be high at #200000~#500000.
Unfortunately, here is the result I put the valid data to the model:
In the abnormal interval, the loss still low.
Here is my code of training model.
I would greatly appreciate it if you kindly give me any suggestions.
scaler = MinMaxScaler(feature_range=(0, 1))
scaler.fit(healthy_data)
data_scaled = scaler.transform(healthy_data)
data_broken_scaled = scaler.transform(broken_data)
timesteps=32
data = data_scaled
dim = 1
data.shape = (-1,timesteps,dim)
lr = 0.0001
Nadam = optimizers.Nadam(lr=lr)
model = Sequential()
model.add(LSTM(50,input_shape=(timesteps,dim),return_sequences=True))
model.add(Dense(dim))
model.compile(loss='mae', optimizer=Nadam ,metrics=['mse'])
EStop = EarlyStopping(monitor='val_loss', min_delta=0.001,patience=150, verbose=2, mode='auto',restore_best_weights=True)
history = model.fit(data,data,validation_data=(data,data),epochs=3000,batch_size=72,verbose=2,shuffle=False,callbacks=[EStop]).history
pred_broken = model.predict(data_broken_scaled)
loss_broken = np.mean(np.abs(pred_broken-data_broken_scaled),axis=1)
fig, ax = plt.subplots(figsize=(20, 6), dpi=80, facecolor='w', edgecolor='k')
ax.plot(range(0,len(loss_broken)), loss_broken, '-', color='red', animated = True, linewidth=1)
I Think, it may better to use Fourier transform to detect anomaly in frequency view.
I means, the train and test data will be converted to frequency domain via Fourier transform. Also I recommend to use Time-windows.
Most AI will act better if they have enough good train data. Your anomaly points should be labeled to 1(by pre-processing step of the data) and it should have high frequency at your time-step(time-windows).
In short, I think your train data may not sufficiently representative for anomaly now.
Pre-trained deep learning models have 2 types of weights associated with them,with top(meaning the dense fully connected layer) and without_top(removing the dense fully connected layer). I want to know how to train a model for the no_top weights
for eg: I have an architecture and I want to create weights for it trained on cifar10 data.So how to train for that?
Since you are talking about CIFAR10, I will give you an example for image classification.
First you get your pre-trained model from Keras without the top layers.
from keras.applications.vgg16 import VGG16
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(48, 48, 3))
Then you add your new layers for the classification tasks.
x = base_model.output
x = Flatten()(x)
x = Dense(512, activation='elu', kernel_initializer='he_normal')(x)
prediction = Dense(10, activation='softmax', kernel_initializer='he_normal')(x)
The last layer has 10 units since CIFAR10 has 10 classes.
You create your model.
model = Model(inputs=base_model.input, outputs=prediction)
You get the CIFAR10 data.
from keras.datasets import cifar10
(Xtrain, Ytrain), (Xtest, Ytest) = cifar10.load_data()
Ytrain = to_categorical(Ytrain)
Ytest = to_categorical(Ytest)
Compile and train.
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(Xtrain, Ytrain, batch_size=64, epochs=1, validation_data=(Xtest, Ytest))
Note: Here the training will change the weights of all your layers (pre-trained layers + new layers). You might want to freeze the pre-trained layers before training to keep their nicely trained weights.
I know how to merge different models into one in Keras.
first_model = Sequential()
first_model.add(LSTM(output_dim, input_shape=(m, input_dim)))
second_model = Sequential()
second_model.add(LSTM(output_dim, input_shape=(n-m, input_dim)))
model = Sequential()
model.add(Merge([first_model, second_model], mode='concat'))
model.fit([X1,X2])
I am not sure how to do this in TensorFlow though.
I have two LSTM models and want to merge those (in the same way as in above Keras example).
outputs_1, state_1 = tf.nn.dynamic_rnn(stacked_lstm_1, model_input_1)
outputs_2, state_2 = tf.nn.dynamic_rnn(stacked_lstm_2, model_input_2)
Any help would be much appreciated!
As was said in the comment, I believe the simplest way to do this is just to concatenate the outputs. The only complication that I've found is that, at least how I made my LSTM layers, they ended up with the exact same names for their weight tensors. This led to an error because TensorFlow thought the weights were already made when I tried to make the second layer. If you have this problem, you can solve it using a variable scope, which will apply to the names of the tensors in that LSTM layer:
with tf.variable_scope("LSTM_1"):
lstm_cells_1 = tf.contrib.rnn.MultiRNNCell(tf.contrib.rnn.LSTMCell(256))
output_1, state_1 = tf.nn.dynamic_rnn(lstm_cells_1, inputs_1)
last_output_1 = output_1[:, -1, :]
# I usually work with the last one; you can keep them all, if you want
with tf.variable_scope("LSTM_2"):
lstm_cells_2 = tf.contrib.rnn.MultiRNNCell(tf.contrib.rnn.LSTMCell(256))
output_2, state_2 = tf.nn.dynamic_rnn(lstm_cells_2, inputs_2)
last_output_2 = output_2[:, -1, :]
merged = tf.concat((last_output_1, last_output_2), axis=1)