I am trying to implement an autoencoder to reconstruct 76 input features using dist-keras.
Here is my code:
features = ['f1', 'f2', ...., 'f76']
assembler = VectorAssembler(inputCols=features, outputCol="features")
dataset = assembler.transform(df)
scaler = MinMaxScaler(inputCol="features", outputCol="features_scaled")
scaler_model = scaler.fit(dataset)
dataset = scaler_model.transform(dataset)
nb_features = len(features)
model = Sequential()
model.add(Dense(50, activation='relu', input_shape=(nb_features,)))
model.add(Dense(nb_features, activation='sigmoid'))
model.summary()
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 50) 3850
_________________________________________________________________
dense_2 (Dense) (None, 76) 3876
=================================================================
Total params: 7,726
Trainable params: 7,726
Non-trainable params: 0
_________________________________________________________________
trainer = SingleTrainer(keras_model=model, worker_optimizer="adam",
loss="mae", features_col="features_scaled",
label_col="features_scaled", num_epoch=5, batch_size=32)
trained_model = trainer.train(dataset)
The training is taking more than 10 hours and still running! Am I missing some thing?
Related
I am trying to run CNN with train MNIST, but test on my own written digits. To do that I wrote the following code but I getting an error in title of this questions:
I am trying to run CNN with train MNIST, but test on my own written digits. To do that I wrote the following code but I getting an error in title of this questions:
batch_size = 64
train_dataset = datasets.MNIST(root='./data/',
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = ImageFolder('my_digit_images/', transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
#print(self.conv1.weight.shape)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv3 = nn.Conv2d(20, 20, kernel_size=3)
#print(self.conv2.weight.shape)
self.mp = nn.MaxPool2d(2)
self.fc = nn.Linear(320, 10)
def forward(self, x):
in_size = x.size(0)
x = F.relu(self.conv1(x))
#print(x.shape)
x = F.relu(self.mp(self.conv2(x)))
x = F.relu(self.mp(self.conv3(x)))
#print("2.", x.shape)
# x = F.relu(self.mp(self.conv3(x)))
x = x.view(in_size, -1) # flatten the tensor
#print("3.", x.shape)
x = self.fc(x)
return F.log_softmax(x)
model = Net()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)
def train(epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = Variable(data), Variable(target)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target)
loss.backward()
optimizer.step()
if batch_idx % 10 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
def test():
model.eval()
test_loss = 0
correct = 0
for data, target in test_loader:
data, target = Variable(data, volatile=True), Variable(target)
output = model(data)
test_loss += F.nll_loss(output, target, size_average=False).data
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
MNIST dataset contains black and white 1-channel images, while yours are 3-channeled RGB probably. Either recode your images or preprocess it like
img = img[:,0:1,:,:]
You can do it with custom transform, adding it after transforms.ToTensor()
The images in training and testing should follow the same distribution. Since MNIST data is by default in Grayscale and it is expected that you didn't change the channels, then the model expects the same number of channels in testing.
The following code is an example of how it's done using a transformation.
Following the order defined below, it
Converts the image to a single channel (Grayscale)
Resize the image to the size of the default MNIST data
Convert the image to a tensor
Normalize the tensor to have same mean and std as that of during training(assuming that you used the same values).
test_dataset = ImageFolder('my_digit_images/', transform=transforms.Compose([transforms.Grayscale(num_output_channels=1),
transforms.Resize((28, 28)),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))]))
My input images have 8 channels and my output (label) has 1 channel and my CNN in keras is like below:
def set_model(ks1=5, ks2=5, nf1=64, nf2=1):
model = Sequential()
model.add(Conv2D(nf1, padding="same", kernel_size=(ks1, ks1),
activation='relu', input_shape=(62, 62, 8)))
model.add(Conv2D(nf2, padding="same", kernel_size=(ks2, ks2),
activation='relu'))
model.compile(loss=keras.losses.binary_crossentropy,
optimizer=keras.optimizers.Adadelta())
return model
Below is the summary of the model implemented above:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 62, 62, 64) 12864
_________________________________________________________________
conv2d_2 (Conv2D) (None, 62, 62, 1) 1601
=================================================================
Total params: 14,465
Trainable params: 14,465
Non-trainable params: 0
_________________________________________________________________
And by another question, I asked here (How to have a 3D filter for Conv2D in Keras?) I am convinced that every channel has their own set of weights. Now, my question is why when I add another channel and increase them 7 to 8, why the results deteriorate?
Prediction accuracy index with 7 channels:
Prediction accuracy index with 8 channels:
I also standardized all 8 channels into values between 0 and 1.
one of the predictions with 7 channels:
one of the predictions with 8 channels:
I have a neural network as below:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_11 (Dense) (None, 36) 288
_________________________________________________________________
dense_12 (Dense) (None, 1) 37
=================================================================
Total params: 325
Trainable params: 325
Non-trainable params: 0
_________________________________________________________________
The activation functions for the first and second layers are "relu" and "sigmoid" respectively.
My problem is the output is a straight line:
I did more investigation and figured out that the weights of this neural net are also a straight line (just first layer).
x_train shape is (2516, 7), and y_train shape is (280, 7)
one of the features (dimensions) of the input data is like below and other are similar to this:
and the labels are like below:
I'm building a neural network to classify images that have an email address written on them. The positive folder contains images with email addresses written on top of the picture, in different fonts, colors, sizes and positions.
The negative folder contains images without text on top and also images with text on top that doesn't have the format of an email address (no # sign).
The pictures are 300 x 225 x 3 (rgb).
It should be a simple a simple classification task (the NN should be able to pick up that when there's an #, the image has an email) but my model isn't performing well. It's stuck at 83% test accuracy after 25 epochs. Also, it's taking 10 hours to train, which sounds excessive to me.
Can you help me to analyse the structure of my CNN and suggest improvements (or help me avoid pitfalls)?
The model I wrote is this:
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.preprocessing.image import ImageDataGenerator
input_size = (64, 48)
# Initialising the CNN
classifier = Sequential()
# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (*input_size, 3), activation = 'relu'))
# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Step 3 - Flattening
classifier.add(Flatten())
# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Part 2 - Fitting the CNN to the images
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('./training_Set',
target_size = input_size,
batch_size = 32,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('./test_set',
target_size = input_size,
batch_size = 32,
class_mode = 'binary')
classifier.fit_generator(training_set,
steps_per_epoch = 8000,
epochs = 25,
validation_data = test_set,
validation_steps = 2000)
I'm trying to visualize the output of each convolutional layer in keras, following this link: MNIST Visualisation. I have modified some layers to remove errors, but now I'm stuck with the Dense Layer Error.
np.set_printoptions(precision=5, suppress=True)
np.random.seed(1337) # for reproducibility
nb_classes = 10
# the data, shuffled and split between tran and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data("mnist.pkl")
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
X_train = X_train.astype("float32")
X_test = X_test.astype("float32")
X_train /= 255
X_test /= 255
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
i = 4600
pl.imshow(X_train[i, 0], interpolation='nearest', cmap=cm.binary)
print("label : ", Y_train[i,:])
model = Sequential()
model.add(Convolution2D(32, 3, 3, border_mode='same',input_shape = (1,28,28))) #changed border_mode from full -> valid
convout1 = Activation('relu')
model.add(convout1)
model.add(Convolution2D(32, 32, 3))
convout2 = Activation('relu')
model.add(convout2)
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(32*196, 128)) #ERROR HERE
Any comment or suggestion highly appreciated. Thank you.
If you check the documentation of a Dense layer then you'll notice that the first argument it accepts is the shape of output and second is init which describes the way how weights of the layer are initiated. In your case you provided the int as second positional argument and this caused error. You should change the code to (assuming that you want an output in a form of 128-dimensional vector):
model.add(Dense(128))