Julia instructions
Help for Julia users in SF2524 & SF3580
This page can be modified both by students and teachers.
You are welcome to improve it.
Something missing? Try contacting gmele at kth se
Skeleton codes for HW3:
Available in files/skeleton_codes_hw3_julia or directly from this link
Skeleton codes for HW2:
Available in files/skeleton_codes_hw2_julia or directly from this link
Minimizing functions
In the hw2 you will need to minimize a function. You can use the package optim.jl Links to an external site. instead of fminsearch Links to an external site. (matlab). Read a bit the documentation in order to have a basic understanding of the functionalities. Here comes an example, we minimize the Rayleigh quotient of a symmetric matrix and we check that the minimum value is an eigenvalue and the minimum point is an eigenvector (known from the theory).
using Optim
A=rand(5,5); A=A+A'
r(x) = (x'*A*x)/(x'*x)
x0 = rand(5)
result = optimize(r, x0, BFGS())
x=result.minimizer
λ=result.minimum
display(norm(A*x-λ*x))
Skeleton codes for HW1:
Available in files/skeleton_codes_hw1_julia or directly from this link
Loading matlab files
MAT Links to an external site. is the Julia package to interface Matlab. Currently, if you try to install in the classical way, it won't work. Follow the following instructions:
julia> ]
(v1.0) pkg> rm MAT
(v1.0) pkg> add https://github.com/halleysfifthinc/MAT.jl#v0.7-update Links to an external site.
using MAT
Then, for loading the data Bwedge.mat of the homework, just run
B=matread("Bwedge.mat")["B"];
Loading matrices from the "gallery"
In Julia we have access to most of the test matrices of the matlab gallery Links to an external site. by simply installing the package MatrixDepot Links to an external site.. The following example shows how to load the matrix of hw1 second exercise. For linux users: you may need to install Zlib 1.2.9.
using Pkg
Pkg.add("MatrixDepot")
using MatrixDepot, Random
nn=10;
Random.seed!(0)
A=matrixdepot("wathen",nn,nn)
Loading and manipulating videos in Julia
This show how you can load an image reshape it into a vector. (For linux users: you may need to install Zlib 1.2.9.
using ImageView, Images, ImageMagick, Printf, FileIO, LinearAlgebra, Arpack
basefilename="market"
# Load the image into an image object, here the first frame
img=load(basefilename*"_0001.png");
# Visualize it
imshow(img)
# Determine the image size
sz=size(img); szv=sz[1]*sz[2];
# Reshape the image to a vector:
R=float(red.(img));
G=float(green.(img));
B=float(blue.(img));
v=[reshape(R,szv,1);reshape(G,szv,1);reshape(B,szv,1)]
One way to do video processing is to separate the frames of the video into separate image files. This has already been done for you in the homework so you can just download the zip-file with images.
(Not necessary for homework: If you want to do the conversion yourself on your own recorded video or laspalmas.mkv or plattan.mkv here, you can generate snapshot images in ubuntu with the commands: export frames_per_second=0.1; ffmpeg -i laspalmas.mkv -r $frames_per_second laspalmas_%04d.png)
The frames can be loaded into a matrix by appropriate for loop
m=5 # Number of frames to load
A=zeros(size(v,1),m) # Matrix to store all the frames in
for k=1:m
fname=@sprintf("%s_%04d.png",basefilename,k);
img=load(fname);
println(fname)
sz=size(img); szv=sz[1]*sz[2];
R=float(red.(img));
G=float(green.(img));
B=float(blue.(img));
v=[reshape(R,szv,1);reshape(G,szv,1);reshape(B,szv,1)]
A[:,k]=v;
end
d
The process to reshape an image into a vector can be reversed (and you need it for the homework).
v=A[:,3]; # Third column of A = third frame
vv=reshape(v,szv,3);
R=reshape(vv[:,1],sz[1],sz[2]);
G=reshape(vv[:,2],sz[1],sz[2]);
B=reshape(vv[:,3],sz[1],sz[2]);
newimg=RGB.(R,G,B)
imshow(newimg)
Common mistakes / problems
If you initiate vectors use concrete types and not abstract types. Instead of creating vectors as Complex-type use Complex64. Avoid Float and use Float64 instead.
julia> n=10000;
julia> x=zeros(ComplexF64,n);
julia> A=randn(n,n);
julia> @time A*x;
0.107388 seconds (6 allocations: 156.484 KiB)
julia> x=zeros(Complex,n);
julia> @time A*x;
9.059851 seconds (494.97 M allocations: 11.847 GiB, 17.18% gc time)
Material used in the tutorial (may be updated)
Look in the folder SF2524/Files/tutorial
If this does not work, use this link Links to an external site.