Every time I train my CNN on matlab, is it remembering the old weights from the previous time I trained it? Or does it reset them?
So for example, I have trained a CNN on my data using a learning rate of 0.0003 and 10 epochs, with a minibatch size of 32. After training it, lets say I get an accuracy of 0.7. Now I want to adjust the learning rate and the minibatch size and try training it again to see how the accuracy changes, using the trainNetwork Matlab function. My question is, is it training the model from scratch or is it training them using the weights previously calculated? I want it to start from scratch to prevent overfitting every time I adjust the hyperparamters. Sorry if this is intuitive and I'm being dumb lol I just wanna make sure.
1 answer
-
answered 2022-05-06 18:08
Ryan Marizza
It will start from scratch each time.
MATLAB does support transfer learning which can be useful if you want to fine tune a pretrained model, but you have to program it to do so. Here's an article on transfer learning in MATLAB (I guess so you can make sure you're not doing it!)
do you know?
how many words do you know
See also questions close to this topic
-
How to sum functions in MATLAB under a condition
I'm trying to write a function that sums a bunch of functions and I have no idea what I'm doing wrong. My code:
function [f_max,x_max] = ftot(l,a,E,J,F) % a and F are arrays f_max = 0; b = l-a; n = length(F); f1 = 0; syms x for j=1:n y1 = piecewise(x<=a(j),1,x>a(j),0); y2 = piecewise(x<=a(j),0,x>a(j),1); f = (F(j)*b(j)*x)*y1 + (F(j)*b(j)*x^3)*y2; f1 = f1 + f; end
Basically I want to create a function that is the sum of
F(j)*b(j)*x
forj=1:n
whenx<=a
andF(j)*b(j)*x^3
whenx>a
. How can I accomplish that? -
Matrix indexing in matlab
a is a nxn matrix.
I have this code:
[m,n] = size(a); x = zeros(m,1); for j=1:1:n if(j==1) a(1,:) = []; else end disp(a); a(:,j) = []; disp(x); disp(a); end
And it gives error in line "a(:,j) = [];" which says Matrix index is out of range for deletion.Why?I dont understand.Help appreciated
-
Separate initialization and display of plot
What is a proper way to separate the initialization and the display of plots in Matlab? (I mean plots in a wide sense here; could be
plot
,plot3
,scatter
etc.) To give a concrete example, I have a pretty complex 3D visualization that usessphere
andmesh
to draw a static sphere mesh and thenscatter3
to plot a moving trajectory on the sphere. To be able to do this in real time I have implemented some simple optimizations, such as only updating thescatter3
object each frame. But the code is a bit messy, making it hard to add additional features that I want, so I would like improve code separation.I also feel like it might sometimes be useful to return some kind of plot object from a function without displaying it, for example to combine it with other plots in a nice modular way.
An example of what I have in mind would be something like this:
function frames = spherePlot(solution, options) % Initialize sphere mesh and scatter objects, configure properties. ... % Configure axes, maybe figure as well. ... % Draw sphere. ... if options.display % Display figure. end for step = 1:solution.length % Update scatter object, redraw, save frame. % The frames are saved for use with 'movie' or 'VideoWriter'. end end
Each step might also be separated out as a function.
So, what is a neat and proper way to do stuff like this? All documentation seems to assume that one wants to display everything right away.
-
Training an ML model on two different datasets before using test data?
So I have the task of using a CNN for facial recognition. So I am using it for the classification of faces to different classes of people, each individual person being a it's own separate class. The training data I am given is very limited - I only have one image for each class. I have 100 classes (so I have 100 images in total, one image of each person). The approach I am using is transfer learning of the GoogLenet architecture. However, instead of just training the googLenet on the images of the people I have been given, I want to first train the googLenet on a separate larger set of different face images, so that by the time I train it on the data I have been given, my model has already learnt the features it needs to be able to classify faces generally. Does this make sense/will this work? Using Matlab, as of now, I have changed the fully connected layer and the classification layer to train it on the Yale Face database, which consists of 15 classes. I achieved a 91% validation accuracy using this database. Now I want to retrain this saved model on my provided data (100 classes with one image each). What would I have to do to this now saved model to be able to train it on this new dataset without losing the features it has learned from training it on the yale database? Do I just change the last fully connected and classification layer again and retrain? Will this be pointless and mean I just lose all of the progress from before? i.e will it make new weights from scratch or will it use the previously learned weights to train even better to my new dataset? Or should I train the model with my training data and the yale database all at once? I have a separate set of test data provided for me which I do not have the labels for, and this is what is used to test the final model on and give me my score/grade. Please help me understand if what I'm saying is viable or if it's nonsense, I'm confused so I would appreciate being pointed in the right direction.
-
What's the best way to select variable in random forest model?
I am training RF models in R. What is the best way of selecting variables for my models (the datasets were pretty big, each has around 120 variables in total). I know that there is a cross-validation way of selecting variables for other classification algorithms such as KNN. Is that also a thing or if there exists a similar way for parameter tuning in RF model training?
-
How would I put my own dataset into this code?
I have been looking at a Tensorflow tutorial for unsupervised learning, and I'd like to put in my own dataset; the code currently uses the MNIST dataset. I know how to create my own datasets in Tensorflow, but I have trouble setting the code used here to my own. I am pretty new to Tensorflow, and the filepath to my dataset in my project is
\data\training
and\data\test-val\
# Python ≥3.5 is required import sys assert sys.version_info >= (3, 5) # Scikit-Learn ≥0.20 is required import sklearn assert sklearn.__version__ >= "0.20" # TensorFlow ≥2.0-preview is required import tensorflow as tf from tensorflow import keras assert tf.__version__ >= "2.0" # Common imports import numpy as np import os (X_train_full, y_train_full), (X_test, y_test) = keras.datasets.fashion_mnist.load_data() X_train_full = X_train_full.astype(np.float32) / 255 X_test = X_test.astype(np.float32) / 255 X_train, X_valid = X_train_full[:-5000], X_train_full[-5000:] y_train, y_valid = y_train_full[:-5000], y_train_full[-5000:] def rounded_accuracy(y_true, y_pred): return keras.metrics.binary_accuracy(tf.round(y_true), tf.round(y_pred)) tf.random.set_seed(42) np.random.seed(42) conv_encoder = keras.models.Sequential([ keras.layers.Reshape([28, 28, 1], input_shape=[28, 28]), keras.layers.Conv2D(16, kernel_size=3, padding="SAME", activation="selu"), keras.layers.MaxPool2D(pool_size=2), keras.layers.Conv2D(32, kernel_size=3, padding="SAME", activation="selu"), keras.layers.MaxPool2D(pool_size=2), keras.layers.Conv2D(64, kernel_size=3, padding="SAME", activation="selu"), keras.layers.MaxPool2D(pool_size=2) ]) conv_decoder = keras.models.Sequential([ keras.layers.Conv2DTranspose(32, kernel_size=3, strides=2, padding="VALID", activation="selu", input_shape=[3, 3, 64]), keras.layers.Conv2DTranspose(16, kernel_size=3, strides=2, padding="SAME", activation="selu"), keras.layers.Conv2DTranspose(1, kernel_size=3, strides=2, padding="SAME", activation="sigmoid"), keras.layers.Reshape([28, 28]) ]) conv_ae = keras.models.Sequential([conv_encoder, conv_decoder]) conv_ae.compile(loss="binary_crossentropy", optimizer=keras.optimizers.SGD(lr=1.0), metrics=[rounded_accuracy]) history = conv_ae.fit(X_train, X_train, epochs=5, validation_data=[X_valid, X_valid]) conv_encoder.summary() conv_decoder.summary() conv_ae.save("\models")
Do note that I got this code from another StackOverflow answer.
-
Print accuracy for YOLOv5 model?
I want to print the accuracy of my trained model. I do understand when I use the model through the camera it shows me the accuracy on the box only, but I want to see the accuracy of my whole model rather than a single instance of my image.
cap = cv2.VideoCapture(0) # loop through labels for label in labels: print('Collecting images for {}'.format(label)) #so that we can see when transitioning through images time.sleep(5) #sleep or wait for 5 seconds when transitioning between image for other class # Loop through image range for img_num in range(number_imgs): print('Collecting images for {}, image number {}'.format(label, img_num)) # webcam feed ret, frame = cap.read() # read the feed from the web cam and store it in given vars # Naming image path imgname = os.path.join(IMAGES_PATH, label+'.'+str(uuid.uuid1())+'.jpg') #Writes out image to file cv2.imwrite(imgname,frame) # Render to screen cv2.imshow('Image Collection', frame) # 2 sec delay between diff captures time.sleep(2) if cv2.waitKey(10) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
!cd yolov5 && python train.py --img 320 --batch 16 --epochs 500 --data dataset.yml --weights yolov5s.pt
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/runs/train/exp/weights/last.pt', force_reload=True) while cap.isOpened(): ret, frame = cap.read() # Make detections results = model(frame) cv2.imshow('YOLO', np.squeeze(results.render())) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() cv2.waitKey(1)
-
keyerror 0 during cpu trainning
I am not sure what is the reason, probably my index exceed the limit of the input? But how can I solve this error? Here is the code I used https://github.com/wanyu-lin/ICML2021-Gem
-
how can I modify Dataset class to make the mask RCNN work with multiple objects?
I am currently working on instance segmentation. I follow these two tutorials:
However, these two tutorials work perfectly with one class like person + background. But in my case, I have two classes like a person and car + background. I didn't find any resources about making the Mask RCNN work with multiple objects.
Notice that:
I am using PyTorch ( torchvision ), torch==1.10.0+cu111 torchvision==0.11.0+cu111 torchaudio==0.10.0
I am using a Pascal VOC annotation
i used segmentation class (not the XML file) + the images
and this is my dataset class
class PennFudanDataset(torch.utils.data.Dataset): def __init__(self, root, transforms=None): self.root = root self.transforms = transforms # load all image files, sorting them to # ensure that they are aligned self.imgs = list(sorted(os.listdir(os.path.join(root, "img")))) self.masks = list(sorted(os.listdir(os.path.join(root, "imgMask")))) def __getitem__(self, idx): # load images ad masks img_path = os.path.join(self.root, "img", self.imgs[idx]) mask_path = os.path.join(self.root, "imgMask", self.masks[idx]) img = Image.open(img_path).convert("RGB") # note that we haven't converted the mask to RGB, # because each color corresponds to a different instance # with 0 being background mask = Image.open(mask_path) mask = np.array(mask) # instances are encoded as different colors obj_ids = np.unique(mask) # first id is the background, so remove it obj_ids = obj_ids[1:] # split the color-encoded mask into a set # of binary masks masks = mask == obj_ids[:, None, None] # get bounding box coordinates for each mask num_objs = len(obj_ids) boxes = [] for i in range(num_objs): pos = np.where(masks[i]) xmin = np.min(pos[1]) xmax = np.max(pos[1]) ymin = np.min(pos[0]) ymax = np.max(pos[0]) boxes.append([xmin, ymin, xmax, ymax]) boxes = torch.as_tensor(boxes, dtype=torch.float32) # there is only one class labels = torch.ones((num_objs,), dtype=torch.int64) masks = torch.as_tensor(masks, dtype=torch.uint8) image_id = torch.tensor([idx]) area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0]) # suppose all instances are not crowd iscrowd = torch.zeros((num_objs,), dtype=torch.int64) target = {} target["boxes"] = boxes target["labels"] = labels target["masks"] = masks target["image_id"] = image_id target["area"] = area target["iscrowd"] = iscrowd if self.transforms is not None: img, target = self.transforms(img, target) return img, target def __len__(self): return len(self.imgs)
anyone can help me?
-
Comparison between object detection algorithm speeds
I am writing my final degree project and I am having trouble to compare different algorithms in the state of the art. I am comparing ResNet, MobileNet SSD, YOLOv4, VGG16, and VGG19 used in embedded devices such as Jetson Nano or Raspberry pi. All algorithms are used for object detection but I am unable to find information about which one is faster or usually gets a higher accuracy. Also, I was looking if they can be used in low-performance devices. I would be grateful if someone is able to help me.
Thanks in advance.
-
Is this a valid approach to scale your target in machine learning without leaking information?
Consider a housing price dataset, where the goal is to predict the sale price.
I would like to do this by predicting the "Sale price per Squaremeter" instead, since it yields better results.
The question is if I implement it like this - does it introduce an information leak in the test set or not?
When I split my dataset in scikit learn:
df= read(Data) target = df["SalePrice"] df.drop(columns=["SalePrice"], inplace=True) X_train, X_test, y_train, y_test = train_test_split(df, target, test_size=0.20)
And then scale y_train:
# Scale target by LivingSpace and call fit() y_train = target/X_train["LivingSpace"] estimator.fit(X_train, y_train)
And use predict and scale the target in y_test to get SalePrice per Squaremeter:
y_pred, y_true = estimator.predict(X_test), y_test/X_test["LivingSpace"]
I think this is valid, since I only scale the target by a known value. It should not make a difference if I predict the
SalePrice
directly orSalePrice / LivingSpace
, since LivingSpace is given to me anyway when I predict the price.If this holds true, we could also directly apply this target transformation to the train and test set and just transform the predicted values back in the end, right?
This should of course then also hold true for any feature given in X. As long as Information about the target itself is NOT present in X I see no problem here. Remember the true target is the SalePrice only, so my intention is to scale it back from sale price per squaremeter. The transformation is just used for better training results.
What are your thoughts about this code?
-
How to correctly generate training data based on percentages?
I have a question.
I am currently generating training data for my bayesian network as follows: (also as code down below)
-> infected stands for people who are infected (0= not infected, 1= infected) -> p_tests is the result of the test (0= test negative, 1= test positive)
Wether there is a 0 or 1 in TP, FN, FP and TN is decided like this:
data = np.random.randint(2, size=(10, 2)) columns = ['infected', 'p_tests'] df = pd.DataFrame(data=data, columns=columns) df["TP"] = ((df['infected'] == 1) & (df['p_tests'] == 1)).astype(int) df["FN"] = ((df['infected'] == 1) & (df['p_tests'] == 0)).astype(int) df["FP"] = ((df['infected'] == 0) & (df['p_tests'] == 1)).astype(int) df["TN"] = ((df['infected'] == 0) & (df['p_tests'] == 0)).astype(int) print(df)
So that is running fine.
My question now would be, how I can decide eg. the 1 and 0 of the infected group based on my probabilities.
The chance to be infected is 10%. How can I program the data, so that 10% of my set have 1s (show that 10% are infected) ?
The same is with the probability of TP (80%), TN(98%), FP(2%), FN(20%)
Does anyone have an Ideo on how to solve this?