trainingSet = imageSet(imgFolder1, 'recursive');
% Using HOG Features
cellSize = [4 4];
hogFeatureSize = length(hog_4x4);
%% Train a Digit Classifier
trainingFeatures = [];
trainingLabels = [];
for digit = 1:numel(trainingSet)
numImages = trainingSet(digit).Count;
features = zeros(numImages, hogFeatureSize, 'single');
for i = 1:numImages
img = read(trainingSet(digit), i);
% Apply pre-processing steps
lvl = graythresh(img);
img = im2bw(img, lvl);
features{i}= extractHOGFeatures(img, 'CellSize', cellSize);
end
labels= repmat(trainingSet(digit).Description, numImages, 1);
trainingFeatures=[training features; features];
trainingLabels=[trainingLabels; labels];
end
Classifier=fitcecoc(trainingFeatures, training labels);
I am trying to train an svm classifier with this code above.
I get error at the classifier line, saying
Error in fitcecoc: obj=fit(temp, X, Y);
...X must be a numeric matrix
I don't know what to do. Pls help
Related
\Dear all,
I am trying to reimplement the code from Kaggle (https://www.kaggle.com/code/tanmay17061/transformers-bert-hidden-embeddings-visualization/notebook). Everything seems fine until I run the cell below:\
loss_function = torch.nn.NLLLoss()
optimizer = AdamW(lr=LEARNING_RATE,params=model.parameters())
liveloss = PlotLosses()
for epoch in range(EPOCHS+1):
print(f'epoch = {epoch}')
logs = {}
if epoch: #do not train on 0th epoch, only visualize on it
model.train(True) #toggle model in train mode
train_correct_preds,train_total_preds,train_total_loss = 0,0,0.0
for x,y in get_bert_encoded_data_in_batches(train_df,BATCH_SIZE,MAX_SEQ_LEN):
model.zero_grad()
sent_ids,masks = x
sent_ids = sent_ids.to(device)
masks = masks.to(device)
y = y.to(device)
model_out = model(sent_ids,masks,return_dict=True)
log_probs = torch.nn.functional.log_softmax(model_out.logits, dim=1)
loss = loss_function(log_probs, y)
loss.backward()
torch.nn.utils.clip_grad_norm\_(model.parameters(), 1.0) #gradient clipping to prevent exploding gradients
optimizer.step()
model.train(False) #toggle model in eval mode
with torch.no_grad():
train_correct_preds,train_total_preds,train_total_loss = 0,0,0.0
train_masks,train_ys = torch.zeros(0,MAX_SEQ_LEN),torch.zeros(0,1)
train_hidden_states = None
for x,y in get_bert_encoded_data_in_batches(train_df,BATCH_SIZE,MAX_SEQ_LEN):
sent_ids,masks = x
sent_ids = sent_ids.to(device)
masks = masks.to(device)
y = y.to(device)
model_out = model(sent_ids,masks,output_hidden_states=True,return_dict=True)
log_probs = torch.nn.functional.log_softmax(model_out.logits, dim=1)
loss = loss_function(log_probs, y)
hidden_states = model_out.hidden_states[1:]
train_total_loss += (loss.detach() * y.shape[0])
train_preds = torch.argmax(log_probs,dim=1)
train_correct_preds += (train_preds == y).float().sum()
train_total_preds += train_preds.shape[0]
train_masks = torch.cat([train_masks,masks.cpu()])
train_ys = torch.cat([train_ys,y.cpu().view(-1,1)])
if type(train_hidden_states) == type(None):
train_hidden_states = tuple(layer_hidden_states.cpu() for layer_hidden_states in hidden_states)
else:
train_hidden_states = tuple(torch.cat([layer_hidden_state_all,layer_hidden_state_batch.cpu()])for layer_hidden_state_all,layer_hidden_state_batch in zip(train_hidden_states,hidden_states))
visualize_layerwise_embeddings(train_hidden_states,train_masks,train_ys,epoch,'train_data')
train_acc = train_correct_preds.float() / train_total_preds
train_loss = train_total_loss / train_total_preds
logs['loss'] = train_loss.item()
logs['acc'] = train_acc.item()
#
val_correct_preds,val_total_preds,val_total_loss = 0,0,0.0
val_masks,val_ys = torch.zeros(0,MAX_SEQ_LEN),torch.zeros(0,1)
val_hidden_states = None
for x,y in get_bert_encoded_data_in_batches(val_df,BATCH_SIZE,MAX_SEQ_LEN):
sent_ids,masks = x
sent_ids = sent_ids.to(device)
masks = masks.to(device)
y = y.to(device)
model_out = model(sent_ids,masks,output_hidden_states=True,return_dict=True)
log_probs = torch.nn.functional.log_softmax(model_out.logits, dim=1)
loss = loss_function(log_probs, y)
hidden_states = model_out.hidden_states[1:]
#logging logic
val_total_loss += (loss.detach() * y.shape[0])
val_preds = torch.argmax(log_probs,dim=1)
val_correct_preds += (val_preds == y).float().sum()
val_total_preds += val_preds.shape[0]
val_masks = torch.cat([val_masks,masks.cpu()])
val_ys = torch.cat([val_ys,y.cpu().view(-1,1)])
if type(val_hidden_states) == type(None):
val_hidden_states = tuple(layer_hidden_states.cpu() for layer_hidden_states in hidden_states)
else:
val_hidden_states = tuple(torch.cat([layer_hidden_state_all,layer_hidden_state_batch.cpu()])for layer_hidden_state_all,layer_hidden_state_batch in zip(val_hidden_states,hidden_states))
visualize_layerwise_embeddings(val_hidden_states,val_masks,val_ys,epoch,'val_data')
val_acc = val_correct_preds.float() / val_total_preds
val_loss = val_total_loss / val_total_preds
logs['val_loss'] = val_loss.item()
logs['val_acc'] = val_acc.item()
if epoch: #no need to learning-curve plot on 0th epoch
liveloss.update(logs)
liveloss.send()
\And, I get the error:
RuntimeError: Expected object of scalar type Float but got scalar type Long for sequence element 1 in sequence argument at position #1 'tensors'
Could you please give me some hints to correct it. I am really appreciate for your help!
\
I want to classify the Cifar-10 dataset using Hierarchical SVM. I know CNN is best choice but I need to preprocess this data and then use hierarchical SVM. I saw one of the post hierarchical classification with SVM but I am still confused for cifar10. I tried the following code for one level of hierarchy but it doesn't satisfy me as I am getting an accuracy of 90% only. See the code below. Any help would be highly appreciated.
rootFolder = 'cifar10Train';
categories = {'Deer','Dog','Frog','Cat','truck','ship','airplane','horse',...
'bird','automobile'};
imds = imageDatastore(fullfile(rootFolder, categories), 'LabelSource',...
'foldernames');
%Load test data
rootFolder = 'cifar10Test';
imds_test = imageDatastore(fullfile(rootFolder, categories), ...
'LabelSource', 'foldernames');
% Hierarchical SVM
% data generation suffix 'T' is used for test dataset
Y = imds.Labels;
YT = imds_test.Labels;
L = length(Y);
LT = length(YT);
X = zeros(32*32*3,L);
XT = zeros(32*32*3,LT);
% New labels for hierarchy
Y1 = (Y=='Deer');
Y2 = (Y=='Dog');
Y3 = (Y=='Frog');
Y4 = (Y=='Cat');
Y5 = (Y=='truck');
Y6 = (Y=='ship');
Y7 = (Y=='airplane');
Y8 = (Y=='horse');
Y9 = (Y=='bird');
Y10= (Y=='automobile');
% for test dataset
Y1T = (YT=='Deer');
Y2T = (YT=='Dog');
Y3T = (YT=='Frog');
Y4T = (YT=='Cat');
Y5T = (YT=='truck');
Y6T = (YT=='ship');
Y7T = (YT=='airplane');
Y8T = (YT=='horse');
Y9T = (YT=='bird');
Y10T= (YT=='automobile');
% train Samples
for i=1:L
img = readimage(imds,i);
X(:,i) = double(img(:));
end
% test data
for i=1:LT
img = readimage(imds_test,i);
XT(:,i) = double(img(:));
end
%First Linear classification
c1 = fitclinear(X',Y1);
pred1 = predict(c1,XT');%
Acc = sum(pred1==Y1T)/LT;'''
I have written another compressed code for it. But the thing that confuse me is that I will be 9 classifiers not a one. Should I expect one classifier which can classify hierarchically all classes.
function [Classifier Accuracy] = HSVM_mine(Num_of_Classes)
% training data rows consitute samples and column features
[Xtrain,Ytrain,Xtest,Ytest] = data_generate(filename_train,filename_test);
Accuracy = zeros(Num_of_Classes,1)
Classifier = {};
Labels = {'class 1','class 2', 'class 3'...}
Num_of_TrainSamples_per_class= m;
m= size(Xtrain,1)/Num_of_Classes;
Num_of_TestSamples_per_class= n;
n= size(Xtest,1)/Num_of_Classes;
for i=1:Num_of_Classes
X1 = Xtrain((1+(i-1)*m):size(Xtrain,1));
Y1 = (Yrain((1+(i-1)*m):size(Xtrain,1))==Ytrain(Labels(i));
XT = Xtest((1+(i-1)*n):size(Xest,1));
YT = (Yest((1+(i-1)*n):size(Xest,1))==Ytest(Labels(i));
Classifier{i} = fitclinear(X1,Y1)
Pred = predict(Classifier{i},XT);
Accuracy(i) = sum(Pred==YT)/length(YT)
end
I am training multiclass logistic regression for handwritting recognition.For function minimization i am using fmin_tnc.
I have implemented gradient function as follows:
def gradient(theta,*args):
X,y,lamda = args;
m = np.size(X,0);
h = X.dot(theta);
grad = (1/m) * X.T.dot( sigmoid(h)-y );
grad[1:np.size(grad),] = grad[1:np.size(grad),] + (lamda/
m)*theta[1:np.size(theta),] ;
return grad.flatten()
#flattened because fmin_tnc accepts list of gradients
This yields correct gradient values for small set example provided below:
theta_t = np.array([[-2],[-1],[1],[2]]);
X_t = np.array([[1,0.1,0.6,1.1],[1,0.2,0.7,1.2],[1,0.3,0.8,1.3],
[1,0.4,0.9,1.4],[1,0.5,1,1.5]])
y_t = np.array([[1],[0],[1],[0],[1]])
lamda_t = 3
But when using checkgrad function from scipy its giving error of 0.6222474393497573
I am not able to trace why this is happening.Because of this may be fmin_tnc is not performing any optimization and always gives optimized parameters equal to initial parameters given.
fmin_tnc function call is as follows:
optimize.fmin_tnc(func=lrcostfunction, x0=initial_theta,fprime = gradient,args=
(X,tmp_y.flatten(),lamda))
As y and theta passed is of form 1-d array having size(n,) it should be converted to 2-d array having size (n,1).This is because 2-d array form is used in gradient function implementation.
Correct implementation is as follow:
def gradient(theta,*args):
#again y and theta reshaped for same reason
X,y,lamda = args;
l = np.size(X,1);
theta = np.reshape(theta,(l,1));
m = np.size(X,0);
y = np.reshape(y,(m,1));
h = sigmoid( X.dot(theta) );
grad = (1/m) * X.T.dot( h-y );
grad[1:np.size(grad),] = grad[1:np.size(grad),] +
(lamda/m)*theta[1:np.size(theta),] ;
return grad.ravel()
I'm writing an NN which requires text (as a string) to be fed in as a placeholder in Tensorflow. I'm having trouble figuring out how to extract the string from the placeholder, which must hold a tensor object. I tried initializing and interactive session and then calling placeholder.eval(), but I got an error because in the initial run, before the text is fed into the placeholder, I got an error because the placeholder was empty. Can anyone give me any pointers on how to do this?
Here's my code for reference.
def train_1(self):
real_image_size = 256
text_input = tf.placeholder(dtype = tf.string)
real_image = tf.placeholder(dtype = tf.float32, shape = (real_image_size, real_image_size, 3))
text_input = text_input[0][0]
all_captions = self.caption_arr
rand_idx = np.random.random()*11788
fake_caption = all_captions[int(rand_idx)]
while text_input == fake_caption:
rand_idx = np.random.random()*len(captions)
fake_caption = all_captions[rand_idx]
fake_image_size = 64
fake_image = self.generator_1(text_input)
real_result_real_caption = discriminator_1(real_image, text_input)
real_result_fake_caption = discriminator_1(real_image, fake_caption)
fake_result = discriminator_1(fake_image, text_input)
dis_loss = tf.reduce_mean(real_result_fake_caption) + tf.reduce_mean(fake_result) - tf.reduce_mean(real_result_real_caption)
gen_loss = -tf.reduce_mean(fake_result)
t_vars = tf.trainable_variables()
d_vars = [var for var in t_vars if 'dis' in var.name]
g_vars = [var for var in t_vars if 'gen' in var.name]
trainer_dis = tf.train.AdamOptimizer(learning_rate = 1e-4).minimize(d_loss, var_list = d_vars)
trainer_gen = tf.train.AdamOptimizer(learning_rate = 1e-4).minimize(g_loss, var_list = g_vars)
# sess = tf.InteractiveSession()
# sess.run(tf.local_variables_initializer())
# sess.run(tf.global_variables_initializer())
# text_input = text_input.eval({text_input : [[""]]})
with tf.Session() as sess:
batch_size = 1
num_of_imgs = 11788
num_epochs = 1000 #adjust if necessary
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
print('Start Training::: ')
for i in range(num_epochs):
print(str(i) + 'th epoch: ')
feeder = pr.FeedExamples()
num_of_batches = int(num_of_imgs/batch_size)
for j in range(num_of_batches):
#Training the Discriminator.
for k in range(5):
train_data = feeder.next_example()
train_image = train_data[0]
txt = train_data[1]
feed_txt = tf.constant([[txt]])
_, dLoss = sess.run([dis_loss, trainer_dis],
feed_dict = {text_input : feed_txt, real_image : train_image})
#Training the Generator.
for k in range(1):
train_data = feeder.curr_example()
train_image = train_data[0]
txt = train_data[1]
_, gLoss = sess.run([gen_loss, trainer_gen],
feed_dict = {text_input : tf.constant([[txt]]), real_image : train_image})
print('Discriminator Loss: ' + str(dLoss))
print('Generator Loss: ' + str(gLoss))
To answer your question:
https://www.tensorflow.org/api_docs/python/tf/placeholder
Inserts a placeholder for a tensor that will be always fed.
Important: This tensor will produce an error if evaluated. Its value
must be fed using the feed_dict optional argument to Session.run(),
Tensor.eval(), or Operation.run().
A placeholder does not have a value other than the value you feed in. That is the difference to a variable.
Although a variable wouldn't make much sense in your case since you are talking about the input. Therefore it is not clear what you are actually trying to achieve.
I would suggest to reduce the example to a minimal example (e.g. single placeholder, variable or operation). It will also help you to understand TensorFlow better.
I am trying to implement Neural Style Transfer using Keras and trying to keep it as simple as possible. While trying to find gradient using backend.gradients() function of keras, it returns [None]. My code is as follows:
content_image = cv2.imread("C:/Users/Max/Desktop/IMG_20170331_103755.jpg")
content_image = cv2.resize(content_image, (512,512))
style_image = cv2.imread("C:/Users/Max/Desktop/starry.jpg")
style_image = cv2.resize(style_image, (512,512))
content_array = np.asarray(content_image, dtype=np.float32)
content_array = np.expand_dims(content_array, axis=0)
style_array = np.asarray(style_image, dtype=np.float32)
style_array = np.expand_dims(style_array, axis=0)
# Constants:
epochs = 1
height = 512
width = 512
num_channels = 3
step_size = 10
content_layer = ['block2_conv2']
style_layer = ['block1_conv2', 'block2_conv2', 'block3_conv3','block4_conv3', 'block5_conv3']
loss_total = backend.variable(0.0)
# VGG16 Model:
model = VGG16(input_shape = [height, width, num_channels],weights='imagenet', include_top=False)
# Defining losses:
def content_loss(Content, Mixed):
content_loss = backend.mean(backend.square(Mixed - Content))
return content_loss
def gram(layer):
flat = backend.reshape(layer, shape=[1, -1])
gram = backend.dot(flat, backend.transpose(flat))
return gram
def style_loss(Style, Mixed):
S_G = gram(Style)
M_G = gram(Mixed)
size = height*width
return backend.sum(backend.square(S_G - M_G)) / (4. * (num_channels ** 2) * (size ** 2))
'''
def denoise(Image):
loss = backend.mean(backend.abs(Image[:,1:,:,:] - Image[:,:-1,:,:]) + backend.abs(Image[:,:,1:,:] - Image[:,:,:-1,:]))
return loss
'''
# Backend Functions:
output_c = backend.function(inputs = [model.layers[0].input] , outputs = [model.get_layer(content_layer[0]).output])
output_s = backend.function(inputs = [model.layers[0].input] , outputs = [model.get_layer(layer).output for layer in style_layer])
content_output = output_c([content_array])
style_output = output_s([style_array])
# Randomly generated image:
Mixed = np.random.uniform(0, 255, [1, height, width, 3]) - 128
# Loop:
for i in range(epochs):
mixed_c = output_c([Mixed])
mixed_c = mixed_c[0]
loss_c = content_loss(content_output[0], mixed_c)
total = []
mixed_s = output_s([Mixed])
for i in range(len(style_layer)):
style = style_loss(style_output[i], mixed_s[i])
total.append(style)
loss_s = backend.sum(total)
#loss_d = denoise(Mixed)
loss_total = w_c * loss_c + w_s * loss_s #+ w_d * loss_d
gradient = backend.gradients(loss_total, Mixed)
gradient = np.squeeze(gradient)
step_size = step_size / (np.std(gradient) + 1e-8)
Mixed -= gradient * step_size
What changes should i make to get the gradients working properly. I am clueless as to what went wrong.
Thanks!
You're taking gradient of Mixed which is a numpy array and not a variable. You need to define a tensor which will then have value of Mixed.
From Keras documentation:
gradients
keras.backend.gradients(loss, variables)
Returns the gradients of variables w.r.t. loss.
Arguments
loss: Scalar tensor to minimize.
variables: List of variables.