Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Introductory Labs

General information for Matlab development.

To fulfill this assignment, you need to submit these files (all packed in one .zip file) into the upload system:

  • answers.txt - answers to the Assignment Questions
  • assignment_basics.m - a script for data initialization, calling of the implemented functions and plotting of their results (for your convenience, will not be checked)
  • matrix_manip.m - a function implementing the matrix manipulation tasks specified in the section Matrix manipulation
  • compute_letter_mean.m and compute_lr_histogram.m - functions specified in the section Simple data task
  • initial1_mean.png, initial2_mean.png and initials_histograms.png - images specified in the section Simple data task

Use template of the assignment. When preparing a zip file for the upload system, do not include any directories, the files have to be in the zip file root.

Beware of using for loops! :)

Information for development in Python

This is highly experimental feature. Use at your own risk.

Standard is to use Matlab. You may skip this info box if you follow the standard rules.

General information for Python development.

To fulfill this assignment, you need to submit these files (all packed in one .zip file) into the upload system:

  • answers.txt - students developing in python can skip answers for first assignment since it is Matlab oriented
  • basics.ipynb - a script for data initialisation, calling of the implemented functions and plotting of their results (for your convenience, will not be checked).
  • basics.py - file with implemented methods:
    • matrix_manip - a method implementing the matrix manipulation tasks specified in the section Matrix manipulation
    • compute_letter_mean and compute_lr_histogram - methods specified in the section Simple data task
  • initial1_mean.png, initial2_mean.png and initials_histograms.png - images specified in the section Simple data task

Use template of the assignment. When preparing a zip file for the upload system, do not include any directories, the files have to be in the zip file root.

Beware of using for loops! :)

MATLAB installation

Follow the instructions on Matlab - faculty license.

STPR toolbox installation

Statistical Pattern Recognition Toolbox contains variety of algorithms which you will implement by yourself during this course (you can use them as an inspiration) and contains also many support functions which we will use throughout the semester.

Download the latest version of the toolbox from https://gitlab.fel.cvut.cz/smidm/statistical-pattern-recognition-toolbox. Unpack the content of the .zip file into your working directory (you should be able to find it next week!). To be able to use the toolbox, you have to set the path to it:

  1. Run Matlab
  2. Change the path to the directory where you unpacked the toolbox (use cd command or the path combo box at right top of the Matlab window)
  3. Running stprpath will set the path and do initialization of the toolbox

Now you can switch to any other directory and still use the functions from the STPR toolbox.

To verify the paths try to run demo_ocr.

MATLAB introduction

We will be using MATLAB programming language during the whole semester. It is necessary for you to be as familiar with it as possible, since the labs are time limited and the tasks are often challenging. It may happen that you spend more time looking for MATLAB syntax of some function instead of solving the problem itself.

For the case you are not too sure about your MATLAB skills, here are few useful links:

Assignment Questions

In most of the assignments there will be a set of simple questions. Answering the questions should help you to solve the assignment. All questions need to be answered correctly to get the points for the assignment.

Fill the correct answers to your answers.txt file.

  1. MATLAB function name to generate matrix of zeros (without parentheses)
  2. Symbol(s) used in MATLAB for matrix product
  3. Symbol(s) used in MATLAB for element-wise product
  4. MATLAB way to generate vector [0 1 2 3 4 5 … 100] (check all correct answers):
    • a) seq(0,100)
    • b) xrange(100)
    • c) 0:100
    • d) <0,100>
    • e) linspace(0,100,101)
  5. Select the second to last (předposlední) row of matrix A (check all correct answers):
    • a) A(:,-1)
    • b) A(-1,:)
    • c) A(:,end-1)
    • d) A(end-1,:)
    • e) A[:,end-1]
    • f) A[end-1,:]

Example answers.txt (bogus answers only, fill the correct ones):

question1 : gen_zero_matrix
question2 : 'x*' # enclose special characters in tick marks, also known as single quotation marks.
question3 : '^'  # enclose special characters in tick marks, also known as single quotation marks.
question4 : [a,d,e]
question5 : [b,f]

Matrix manipulation in MATLAB

In the first part of today’s assignment, you will start with some simple matrix manimulation tasks.

AVOID THE USE OF ANY LOOPS IN YOUR PROGRAM!

Your goal is to complete a function output = matrix_manip(A, B), where A and B are input matrices and output is a resulting structure containing results of operations described below.

To have some data to work with, use matrices A and B from assignment_basics.m:

A = [16     2     3    13;
      5    11    10     8;
      9     7     6    12;
      4    14    15     1]
 
B = [ 3     4     9     4     3     6     6     2     3     4;
      9     2    10     1     4     3     7     1     3     5]

Function should work on general input matrices, not only for above prescribed A and B matrices or matrices with the same dimensions. Dimensions of the input matrices will be always suitable for all of the following tasks.

  1. Find the transpose of the matrix A and return it in output.A_transpose. Example result:
    >> output.A_transpose
     
    output.A_transpose =
     
       16     5     9     4
        2    11     7    14
        3    10     6    15
       13     8    12     1
    Hint: Search documentation with lookfor command.
  2. Select the third column of the matrix A and return it in output.A_3rd_col.
    >> output.A_3rd_col
     
    output.A_3rd_col =
     
          3
         10
          6
         15
    Hint: You need to use matrix indexing with colon : character.
  3. Select last two rows from last three columns of the matrix A and return the matrix in output.A_slice.
    >> output.A_slice
     
    output.A_slice =
     
          7   6  12
         14  15   1
  4. Find all positions in A greater then 3 and increment them by 1. Afterwards add column of ones to the matrix. Save the result to output.A_gr_inc.
    >> output.A_gr_inc
     
    output.A_gr_inc =
     
         17     2     3    14     1
          6    12    11     9     1
         10     8     7    13     1
          5    15    16     1     1
    Hint: Try > operator on the whole matrix. Use [] to construct the new matrix and ones function to create a vector with ones.
  5. Create matrix C such that $C_{i,j} = \sum_{k=1}^n A\_gr\_inc_{i,k} \cdot (A\_gr\_inc^T)_{k,j}$ and store it in output.C.
    >> output.C
     
    output.C = 
     
        499   286   390   178
        286   383   351   396
        390   351   383   296
        178   396   296   508
    Hint: No loops are needed, try it on a paper with a 2×2 matrix.
  6. Compute $\sum_{c=1}^n c \cdot \sum_{r=1}^m A\_gr\_inc_{r,c}$:
    >> output.A_weighted_col_sum
     
    output.A_weighted_col_sum =
     
        391
    Hint: You will need colon : special character to generate ranges, per element array multiplication .* and sum() function.
  7. Subtract a vector $(4,6)^T$ from all columns of matrix B. Save the result to matrix output.D.
    >> output.D
     
    output.D = 
     
         -1     0     5     0    -1     2     2    -2    -1     0
          3    -4     4    -5    -2    -3     1    -5    -3    -1
    Hint: Use repmat command to replicate a matrix. (not needed in Matlab > 2016b due to implicit expansion)
  8. Select all column vectors in the matrix D, which have greater euclidean length than the average one.
    >> output.D_select
     
    output.D_select =
     
            0     5     0    -2
           -4     4    -5    -5
    Hint: You can find useful element-wise power of two .^2 and find function that returns indices of non zero elements of a matrix. Euclidean length of a vector can be calculated with the norm function, but for calculating length of all column vectors in a matrix you need to do it with your own code. No loops are needed!

Simple data task in MATLAB

In this part of the assignment, you are supposed to work with a simple input data which contains images of letters. We will use similar data structures later on during the labs. Do the following:

Note: Plotting and displaying of images should be implemented only in assignment_basics.m.

  1. In the data_33rpz_cv01.mat data file, the following variables are stored:
    • images (3D array of 2000 10×10 grayscale images)
    • Alphabet (letters in the images, not full alphabet is included)
    • labels (indexes of the images into Alphabet array).
  2. Inspect all images at once with the code:
    ims = permute(images,[1 2 4 3]); % The images must be ordered by the 4th dimension. (not needed in new matlab versions)
    montage(ims); colormap gray;
    Hint: check documentation for functions imshow, imagesc, subplot, montage.
  3. For a given letter, compute its mean image. This means taking all images in the dataset displaying that letter, and making pixel-wise mean. Use your name initials (if present in the dataset) and save them as initial1_mean.png and initial2_mean.png (use any letter if any of your initials is not present in the dataset). You will probably encounter a problem with non-decimal numbers in the image matrix. You can avoid it by using the uint8 function before returning the mean image.
    • For the purpose of mean image calculation, complete the function:
      letter_mean = compute_letter_mean(letter_char, Alphabet, images, labels)
      where letter_char is a character (e.g. 'A', 'B', 'C') representing the letter whose mean we want to compute, Alphabet, images and labels are loaded from the provided data, and letter_mean is the resulting mean image.
    1. python hint: You have to first round the values, then convert them to uint8.
  4. Compute an image feature x - a sole number characterizing an image. It is defined as:
    x = sum of pixel values in the left half of image - sum of pixel values in the right half of image
    Then make a histogram of feature values of all images of a letter. Complete a function for the feature histogram computation:
    lr_histogram = compute_lr_histogram(letter_char, Alphabet, images, labels, num_bins)
    where letter_char is a character representing the letter whose feature histogram we want to compute, Alphabet, images and labels are loaded from the provided data, num_bins is the number of histogram bins and lr_histogram is the resulting histogram (num_bins long vector containing counts of items in the corresponding bins).
    • For reference the following histogram was computed for letter A with 10 bins:
      >> compute_lr_histogram('A', Alphabet, images, labels, 10)
       
      ans =
       
           1     1     3     6    12    27    24    20     5     1
    • Hint: use hist function to compute the histogram.
    • Plot feature histograms of your initials into one figure to compare them and save the figure as initials_histograms.png.
      • WARNING: make sure you use correct x-axis on the plot. (is it 1-10, or something in orders of 1000s?)
    • Python hint: use matplotlib bar function.
courses/be5b33rpz/labs/01_intro/start.txt · Last modified: 2018/10/12 12:11 by serycjon