Can I use google colab for running matlab codes

87 visualizzazioni (ultimi 30 giorni)
I am working on a deep learning code using vgg16, but I am facing difficulties in excuting it on my computer as it takes a long time. So I tried using google colab but it did not work :(

Risposta accettata

Mike Croucher
Mike Croucher il 27 Giu 2025
Spostato: Chen Lin il 18 Set 2025
You can get MATLAB on Colab but it's very experimental at the moment. Using MATLAB on Google Colab » The MATLAB Blog - MATLAB & Simulink

Più risposte (2)

the cyclist
the cyclist il 29 Ott 2022
I don't have a definitive answer for you, but I'm pretty confident that the answer is -- No.
MATLAB is definitely not made available by default by Google Colab. (Dare I say, this is obvious, given that MATLAB is proprietary software, not open source.)
There is a very nice MATLAB integration for Jupyter, which implements the ability to launch a MATLAB kernel from a Jupyter notebook, but I'm as sure as I can be that that would only work locally, and that one cannot create a MATLAB kernel on Colab.
In principle, I can imagine that it is possible to install MATLAB on the Colab instance, but this seems very unlikely. I did not try.
If your code will run in Octave (an open-source MATLAB clone), then it seems you can run Octave on Colab, and that could work. But, it seems unlikely that a full-blown deep learning algorithm will work in Octave (especially if you are using functions from the Deep Learning Toolbox).
You could try MATLAB Online, which runs MATLAB in the cloud. The Deep Learning Toolbox is available there, but I'm guessing you don't really get a huge increase in resources available to you. (I haven't used it in a long time, but it was kinda slow and clunky when I did.)
Finally, you definitely can run MATLAB in the cloud (e.g. AWS or Azure). But, that will cost $, of course.

PULICHERLA
PULICHERLA il 11 Set 2025
% StrRemNet_main.m
% Complete self-contained code for StrRem-Net in MATLAB R2023b
% Requires Deep Learning Toolbox + Image Processing Toolbox
% Author: Your Project Team
clear; clc; close all;
%% ================= USER SETTINGS =================
root = pwd; % current folder
trainRainDir = fullfile(root,'data','train','rainy');
trainCleanDir = fullfile(root,'data','train','clean');
testRainDir = fullfile(root,'data','test','rainy');
testCleanDir = fullfile(root,'data','test','clean');
inputSize = [256 256 3];
batchSize = 4;
maxEpochs = 5; % ⚠️ increase (e.g. 50–100) for real training
learnRate = 1e-3;
%% ================= DATASET =================
dsTrain = pairedDatastore(trainRainDir,trainCleanDir,inputSize);
dsTest = pairedDatastore(testRainDir,testCleanDir,inputSize);
%% ================= NETWORK =================
lgraph = buildStrRemNet(inputSize);
net = dlnetwork(lgraph);
%% ================= TRAINING LOOP =================
mbq = minibatchqueue(dsTrain, ...
"MiniBatchSize",batchSize, ...
"MiniBatchFcn",@(x,y) preprocess(x,y,inputSize), ...
"MiniBatchFormat",{'SSCB','SSCB'});
iteration = 0;
avgGrad = []; avgSqGrad = [];
for epoch = 1:maxEpochs
reset(mbq);
while hasdata(mbq)
iteration = iteration + 1;
[X,T] = next(mbq);
[loss,gradients] = dlfeval(@modelGradients,net,X,T);
[net,avgGrad,avgSqGrad] = adamupdate(net,gradients,avgGrad,avgSqGrad,iteration,learnRate);
if mod(iteration,5)==0
disp("Epoch "+epoch+" Iter "+iteration+" Loss="+gather(extractdata(loss)));
end
end
[p,s] = evaluate(net,dsTest,inputSize);
fprintf("Epoch %d done — PSNR=%.2f dB, SSIM=%.4f\n",epoch,p,s);
end
save("StrRemNet_trained.mat","net");
%% ================= DEMO: Single Image =================
testImg = imread(fullfile(testRainDir,dir(testRainDir).name));
outImg = inferSingle(net,testImg,inputSize);
figure; imshowpair(testImg,outImg,'montage'); title("Rainy (left) vs Cleaned (right)");
%% ================= FUNCTIONS =================
function ds = pairedDatastore(rainDir,cleanDir,targetSize)
rain = imageDatastore(rainDir);
clean = imageDatastore(cleanDir);
ds = combine(rain,clean);
ds = transform(ds,@(d) preprocessPair(d,targetSize));
end
function dataOut = preprocessPair(data,targetSize)
r = im2single(imresize(imread(data{1}),targetSize(1:2)));
c = im2single(imresize(imread(data{2}),targetSize(1:2)));
dataOut = {r,c};
end
function [X,T] = preprocess(rain,clean,targetSize)
rb = cat(4,rain{:}); cb = cat(4,clean{:});
for i=1:size(rb,4)
rb(:,:,:,i) = imguidedfilter(rb(:,:,:,i)); % guided filter
end
X = dlarray(single(rb),"SSCB");
T = dlarray(single(cb),"SSCB");
end
function lgraph = buildStrRemNet(inputSize)
enc = [
imageInputLayer(inputSize,"Name","input","Normalization","none")
convolution2dLayer(3,32,"Padding","same"), reluLayer
maxPooling2dLayer(2,"Stride",2)
convolution2dLayer(3,64,"Padding","same"), reluLayer
maxPooling2dLayer(2,"Stride",2)
convolution2dLayer(3,128,"Padding","same"), reluLayer];
dec = [
transposedConv2dLayer(4,64,"Stride",2,"Cropping","same"), reluLayer
convolution2dLayer(3,64,"Padding","same"), reluLayer
transposedConv2dLayer(4,32,"Stride",2,"Cropping","same"), reluLayer
convolution2dLayer(3,32,"Padding","same"), reluLayer];
refine = [
convolution2dLayer(3,32,"Padding","same"), reluLayer
convolution2dLayer(3,32,"Padding","same"), leakyReluLayer(0.1)
convolution2dLayer(1,3,"Padding","same","Name","ref_out")];
convlstm = [
convolutionalLSTM2dLayer([3 3],16,"Padding","same","OutputMode","sequence")
convolution2dLayer(3,3,"Padding","same","Name","final_conv")];
lgraph = layerGraph([enc;dec;refine;convlstm]);
end
function [loss,gradients] = modelGradients(net,X,T)
Y = forward(net,X);
loss = mean((Y-T).^2,"all"); % MSE
gradients = dlgradient(loss,net.Learnables);
end
function [meanPSNR,meanSSIM] = evaluate(net,ds,inputSize)
reset(ds); ps=[]; ss=[];
while hasdata(ds)
d = read(ds);
rain = im2single(imresize(imread(d{1}),inputSize(1:2)));
clean = im2single(imresize(imread(d{2}),inputSize(1:2)));
rain = imguidedfilter(rain);
out = predict(net,dlarray(rain,"SSCB"));
out = gather(extractdata(out)); out = min(max(out,0),1);
ps(end+1)=psnr(out,clean); ss(end+1)=ssim(out,clean);
end
meanPSNR=mean(ps); meanSSIM=mean(ss);
end
function outImg = inferSingle(net,img,inputSize)
img = im2single(imresize(img,inputSize(1:2)));
img = imguidedfilter(img);
out = predict(net,dlarray(img,"SSCB"));
outImg = im2uint8(gather(extractdata(out)));
end

Categorie

Scopri di più su Image Data Workflows in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by