Skip to content

Latest commit

 

History

History
205 lines (171 loc) · 24.8 KB

lab_intro-to-python-lab.md

File metadata and controls

205 lines (171 loc) · 24.8 KB

Notes: Frequently Asked Questions for Classifying Images Lab

These notes pertain to Frequently Asked Questions (FAQ) for the 2. Intro to Python, Lesson 6. Lab: Classify Images that were posted and addressed on AIPND slack. We recommend that you review these notes prior to starting the Classify Images Lab to help clarify potential points of confusion regarding the lab.  
 

Quick Links to Frequently Asked Questions

Approaching and Completing the Lab

This lab provides the student with the experience of solving a much larger, more complex problem using python. This lab will require the student write a few hundred lines of code as compared to the fewer lines of code needed to complete the quizzes of the lessons. This lab will increase a student's experience using python to solve a complex problem, especially for students that are newer to python and software engineering. Additionally, this lab should better prepare students for completing the project. The percent viewed of the lab has no bearing upon student graduation from the AIPND; therefore, students are free to use as much or as little help as they need from the materials provided.

To best address all levels of experience with programming and python, the lab may contain more information than is needed to complete the lab for some more experienced students. The instructor suggests students focus on the top part of each section of the lab labeled Coding within the check_images.py.

Coding within the check_images.py provides:

  • Code to Edit - The parts of check_images.py (or check_images_hints.py) students will be editing for that section
  • Expected Outcome - The expected outcome once the edits are completed and the program is run
  • Checking your code - How to best check that the edits provided the expected outcome.

All parts of the section after Coding within the check_images.py are there to provide:

  • Additional information and code about the python modules that were used in the instructor solution
  • Additonal Information about files and functions that were used in the instructor solution
  • The GitHub repository link in Code section to the code displayed within that section
  • The solutions video in Solutions Video

Be aware that some times more information is provided than will be used in the solution, as a means to provide complete examples that aren't necessarily the exact same as those that will be used in the lab solution. Additonally, the instructor solution breaks certain coding concepts into multiple lines of code as to provide a solution that is easier to understand to the beginning programmer. The instructor solution also contains data checks that verify the data is in proper format prior to it's use within the program. These are left as examples to the student as techniques that can be used when dealing with real data that may not conform to an expected format.  
 

Choosing Between the Two Versions of the Lab

Students have to choose between two versions of the lab. Based upon student feedback, we have provided check_images_hints.py which is the same as check_images.py except check_images_hints.py:

  • Provides more of the instructor solution, so there is less for the student to code on their own.
  • Provides detailed directions on the sections that the student is expected to code on their own.

check_images_hints.py was designed for students that:

  • Have less time to devote to the lab,
  • Are new to Python and/or programming, or
  • Feel less comfortable with programming in Python.

Both check_images.py and check_images_hints.py will reach the same solution, so the solutions videos at the end of each section apply to both. It is left for the student to decide which version of the lab (check_images.py or check_images_hints.py) that they want to complete. Please review 3. Lab Instructions for more details.  
 

"Checking your code" using Provided Functions

Students can use the functions within the program print_functions_for_lab_checks.py to check their code for sections 7.Command Line Arguments through 14. Calculating Results. Students will find this program within the Lab Workspace and within the GitHub repository (see link above). The docstring below each function definition within print_functions_for_lab_checks.py, describes how to use the function. Additionally, one can find each function used within the Lab Solution, check_images_solution.py.

How to Check check_images.py

If your check_images.py program does not have the following import statement, then your check_images.py doesn't have access to the functions in print_functions_for_lab_checks.py. This import statement should occur around line 30 in the check_images.py program.

# Imports print functions that check the lab
from print_functions_for_lab_checks import *

How to update check_images.py to use these Functions

For students that began editing check_images.py prior to when these functions were made available in the workspace; you will need to do the following to have access to the print_functions_for_lab_checks.py functions.

  • Add the following import statement after the statement from classifier import classifier but before def main(): in check_images.py.
# Imports print functions that check the lab
from print_functions_for_lab_checks import *

5. Lab Workspace is Missing check_images_hints.py

For students that began editing check_images.py prior to when the program check_images_hints.py was made available in the workspace; you will need to do the following to have access to it.

Upload the following files into the 5. Lab Workspace

Issues with the Lab Workspace

While it is recommended that you work on the lab within the 5. Lab Workspace, a few students may experienced issues trying to work within the Lab Workspace. Some students in the April cohort found these issues resolved when they switched their internet browsers. Specifically, some students found that Chrome worked best; while others found that Firefox worked better for them. If you are running into the problem where the files in the workspace don't load and/or running code within the workspace runs extremely slowly; please try the following:

  • Quit and exit out of the web browser you are using, then open it back up and restart it.
  • Switch to a different web browser.

If you run into issues with the 5. Lab Workspace and the above recommendations didn't work; alternativIy, you are welcome to complete the lab on a local computer using the instructions in the next FAQ.  
 

Running the Lab on a Local Computer

While it is recommended that you work on the lab within the 5. Lab Workspace, to run the lab on a local computer, you will have needed to have python 3.6 intalled on your computer.

Installing Anaconda

The easiest way to install python and the appropriate python modules is to install Anaconda. You will also have found the directions to install Anaconda in 2. Intro to Python, Lesson 5. Scripting, Section 3. Install Python Using Anaconda.

Installing PyTorch and torchvision

Linux or OSX(Mac)

For this lab you will also need to install the python packages pytorch and torchvision. If your local computer has a Linux or OSX (Mac) operating system look to Get Started. for installation instructions.

Windows

With the release of PyTorch v0.4.0, this version of PyTorch supports installation on the Window Operating Systems. To install PyTorch v0.4.0 or higher look to Get Started. for installation instructions.
The instructions below are for installing versions of PyTorch that are 0.3.1v or older.

Windows 7 - for Pytorch v0.3.1 or older

conda install -c peterjc123 pytorch-cpu
pip install torchvision

Windows 10 - for Pytorch v0.3.1 or older

With acknowledgement to Giu of the April Cohort for providing the installation commands.

conda install -c peterjc123 pytorch cuda90
pip install torchvision

With acknowledgement to Oliver E. of the April Cohort for providing alternative methods to run the pytorch and torchvision on Windows 10 using Linux environment. Other options are to install Windows Substyem for Linux (WSL) or VMware to install Linux on a virtual environment. For directions for installation of WSL and for installation of VMware.  
 

Files Required to Run check_images.py or check_images_hints.py Locally

The following files and folders need to be put in the same folder as the check_images.py (or check_images_hints.py) python program on your local computer. You will find these files and folders within the GitHub AIPND Repository. There are more programs in the repository than you will need, these extra programs are there to provide the code within the lessons in a format that can be copied and pasted from.

Needed Files:

  • pet_images (folder of 40 pet image)
  • classifier.py (classifier function you will be using to classify the images)
  • dognames.txt (file that contains all the valid dog names from the classifier function and the pet image files)
  • imagenet1000_clsid_to_human.txt (dictionary that converts the classifier function ids to text labels)
  • run_models_batch.sh (a bash script that will run check_images.py sequentially for all 3 model architectures and output their results to text files - on Unix/Linux/OSX/Lab Workspace from a terminal window)
  • run_models_batch_hints.sh (a bash script that will run check_images_hints.py sequentially for all 3 model architectures and output their results to text files - on Unix/Linux/OSX/Lab Workspace from a terminal window)
  • run_models_batch.bat (a batch script that will run check_images.py sequentially for all 3 model architectures and output their results to text files - on Windows from the Anaconda Prompt window)
  • run_models_batch_hints.bat (a batch script that will run check_images_hints.py sequentially for all 3 model architectures and output their results to text files - on Windows from the Anaconda Prompt window)
  • test_classifier.py (an example program that demonstrates how to use the classifier function)
  • print_functions_for_lab_checks.py (a program that contains functions that will allow you to check your code ("Checking your Code) for 7. Command Line Arguments through 14. Calculating Results sections of the lab)

Also be aware the instructor provided solution also exists within the GitHub repository as the file check_images_solution.py and the file run_models_batch_solution.sh can be used to run the instructor solution for all 3 model architectures in a Unix/Linux/OSX/Lab Workspace environment from a terminal window. For Windows, you will be using the file run_models_batch_solution.bat from the Anaconda Prompt window (see next FAQ for details).  
 

Running Batch Files on Windows OS Locally

To run the files run_models_batch, run_models_batch_hints, or run_models_batch_solution that run all 3 model architectures using check_images.py, check_images_hints.py, or check_images_solution.py on a Windows OS locally; you will need to use the files that end with the extention .bat instead of the extension .sh. You will have also needed to have installed Anaconda on your computer (see following FAQ for details on Anaconda installation).

Directions:

  • Open the Anaconda Prompt - either from typing Anaconda Prompt within the search bar and selecting it or by clicking on it once it's found within the Anaconda folder of programs.
  • Navigate to the folder within the Anaconda Prompt that contains the Lab files including check_images.py and run_models_batch.bat using the command cd.
  • Type the command within the Anaconda Prompt:
run_models_batch.bat

If instead you are working with the check_images_hints.py program, you will replace all instances of run_models_batch.bat from the directions above with run_models_batch_hints.bat. If instead you are running the instructor solution, check_images_solution.py, you will replace all instances of run_models_batch.bat from the directions above with run_models_batch_solution.bat.  
 

Forking the GitHub Respository for Solution display and debugging

We recommend forking the github repository for AIPND to display your solution and to allow for improved debugging support. See the following for directions on joining GitHub and for direction on how to fork a GitHub repository. Additionally, forking the repository will enable you to more easily show AIPND instructors and students where you are having difficulty with coding the lab. One can always provide others a link to the code that seems to have errors within their fork.  
 

Eliminating Syntax Errors with Text Editor/Integrated Development Environment

If you are experiencing a lot of syntax errors with your code, you may consider downloading your code and looking at it with your favorite text editor/IDE to help eliminate the syntax errors from your program. Recall in 2. Intro to Python, Lesson 5. Scripting, Section 6. Programming Environment Setup you were provided with a number of text editors that are available to use with python (like Atom, Sublime Text, Notepad++). Additionally, when you installed Anaconda, the Spyder IDE (Integrated Development Environment) for python should be available through the Anaconda Navigator.  
 

Cutting and Pasting Code in the Classroom

If you cut and paste code directly from the classroom, it is very likely you will generate syntax errors with the single and double quotes. This is because the font type differences. If you are going to cut and paste code from the classroom, you will need to erase and replace any copied double or single quotes. Additionally, cutting and pasting code from the classroom may also result in issues regarding the proper code indention; therefore, it is not recommended to cut and paste code directly from the classroom.  
 

Indention of Python Code

Indention is used within Python to distinquish between blocks of code; whereas, with other programming languages, like Java and C++, they may have used curly brackets. The PEP8 Style guide provides the standard for python code and is what has been used for the programs within the Github respository and the Lab workspace. The PEP8 standard for indention is to use 4 spaces for each indention level. Not using 4 spaces for indention when editing check_images.py, will likely result in syntax errors.

Be aware that using the tab key within most text editors might not guarentee the proper 4 space indention. Additionally, not all text editors (including the Lab Workspace) provide the proper 4 space indention as is used in the python programs within the repository for this lab.  
 

Formatting Print Statements

Beginning with the solutions video for 10. Creating Pet Image Labels you will notice that the % symbol has been used within the print statements to allow for a formated print statement using the old-style of print formatting. Learning more about print formatting allows for nicer appearing output, but is not a requirement of this lab.

One can learn more about formatting one's print statements using:

Replacing Pass Statements

When editing the functions provided in check_images.py you will need to replace the pass statement with your code for that function. The pass statement does nothing, it's used so that the program will still run eventhough the functions have not been fully defined.  
 

Alternative Solution for 11. Classifying Images - Part 1

With acknowledgement to Shawn M. of the April Cohort for pointing out that the in operation also handles string comparison. This led to the discovery of a more simple solution using the in operation.

The in operation can be used to find exact match of a string as compared to a list of different strings. Because the classifier function can return a list of terms for a classification (like classifier label: "dalmatian, coach dog, carriage dog"); one can use python's split function to split a returned list of terms into a list using the separator ", ". This allows the use of the in operation to determine if a pet label is contained as an exact match to one of the classifier label's terms (like pet label: "dalmatian" matching classifier label: "dalmatian, coach dog, carriage dog").

There are cases when a classifier label term that contains at least one word that does match one of our pet labels like a classifier term of "tabby cat" or "egyptian cat" matching to pet label "cat" or classifier term of "standard poodle" matching to pet label "poodle". To address these cases one can use python's split function to split the classifier label term into multiple words and then check for matches using the in operation.

To see the alternative solution look at the github program alternative-to-classify_images.py.