stock-photo-recognizer / kaggle code backup - stock photo recognizer.txt
zachwormgoor@gmail.com
raw kaggle backup
7cafc37
Stock photo retriever
The goal of this project was to experiment and learn the first lesson of fast.ai, as well as simple data mining. The topic chosen was to be able to tell if a photo was a "stock" photo, more specifically product photos, versus an amateur item photo. This could have applications in determining information about an article or webpage from the types of images it contains.
This topic was chosen as an example of something expected to be more difficult than the basic provided examples (is this a bird? is this a cat?), since stock vs. amateur photo differences are as much about the focus, lighting, contrast, etc. as they are about the subject. We can explore whether any of the image models can distinguish these more qualitative aspects of images rather than the typical contained objects, and we can try to get a feel for how much training data might be required to get any amount of accuracy with this technique.
First, Craigslist was chosen to get examples of amateur photos as a way of exploring data mining (rather than using the DDG search in the example from fast.ai). This proved to have some interesting challenges. In the last couple years Craigslist appears to have changed their website to dynamically load content only when the page is visibly rendered (perhaps to save bandwidth, or perhaps to mitigate bots), and some online examples of scraping appear to predate this change. So techniques that closely mimic how a human would use a browser were needed, rather than simply making html requests for a page.
Selenium was used to scrape the page, it is not expected that this would work through online execution such as Kaggle, so the following code would have to be run locally.
# Note: The following probably won't run on an online hosted Jupyter notebook,
# you'll have to copy the code and run locally.
import selenium.webdriver
from selenium.webdriver.common.by import By
def get_first_page_of_images_from_cragslist(craigslist_url):
# Load page we want to scrape
driver = selenium.webdriver.Firefox()
driver.get(craigslist_url) # various stuff in Virginia
# Function to scroll to the bottom of a Craigslist page
def scroll_for_a_while():
last_height = driver.execute_script("return document.body.scrollHeight")
outer_frame = driver.find_element(By.CLASS_NAME, "cl-search-results")
page_loc = last_height
for i in range(18):
print("page down")
driver.execute_script("window.scrollByPages(1);") # firefox only
time.sleep(.2) # .01 seems to be too fast...window size, PC speed, and internet speed would all affect what speeds work here.
# Scroll and then retrieve page source
# Need to wait till page is completely loaded, subsequent retrievals fail otherwise.
# proper solution is this: https://stackoverflow.com/a/26567563
# but would have to refactor away from BeautifulSoup.
# for expediency, just doing a hard wait.
print("loaded page, but waiting several seconds for page to load...")
import time
from threading import Thread
time.sleep(7)
print("scrolling to bottom")
scroll_for_a_while()
print("parsing page")
page_text = driver.page_source
# Parse page
# Credit to Riley Predum's Craigslist scraping example, portions taken from it:
# https://github.com/rileypredum/East-Bay-Housing-Web-Scrape/blob/master/EB_Apt_Prices_Final.ipynb
# Note that this could be done with Selenium calls instead.
from bs4 import BeautifulSoup
html_soup = BeautifulSoup(page_text, 'html.parser')
posts = html_soup.find_all('li', class_= 'cl-search-result cl-search-view-mode-gallery')
print(type(posts)) #to double check that I got a ResultSet
print(len(posts)) #to double check I got 120 (elements/page)
# Examples of scraping some bits of data to verify operation. Not used at present.
# Retrieve the first post
post_one = posts[0]
imageUrl = post_one.img['src']
# Price of the first post
post_one_price = post_one.find('span', class_= 'priceinfo').text
post_one_price.strip()
# Time / date, meta data
post_one_time = post_one.find('div', class_= 'meta').text
#post_one_datetime = post_one_time['datetime']
# Title
post_one_title = post_one['title']
post_one_link = post_one.a['href']
# Finally, retrieve all of the post imgs and aggregate into list:
def get_img_url_from_post(post):
return post.img['src']
# Should be 120 URLs of imgs in this list:
post_img_urls = map(get_img_url_from_post, posts)
driver.close() # don't open multiple browsers with multiple calls
return post_img_urls
amateur_photos_virginia = get_first_page_of_images_from_cragslist("https://blacksburg.craigslist.org/search/sss?hasPic=1")
amateur_photos_oregon = get_first_page_of_images_from_cragslist("https://corvallis.craigslist.org/search/sss?hasPic=1")
amateur_photos_pennsylvania = get_first_page_of_images_from_cragslist("https://scranton.craigslist.org/search/sss?hasPic=1")
# "https://sfbay.craigslist.org/search/eby/apa?hasPic=1&availabilityMode=0") # rental properties
print("\r\n# Virginia Craigslist photos:")
print("amateur_photos_virginia = ['")
print(*list(amateur_photos_virginia), sep="',\r\n '") # if this throws an exception, probably the scrolling did not work to get the page to load all the images...try slowing down the scrolling.
print("']")
print("\r\n# Oregon Craigslist photos:")
print("amateur_photos_oregon = ['")
print(*list(amateur_photos_oregon), sep="',\r\n '")
print("']")
print("\r\n# Pennsylvania Craigslist photos:")
print("amateur_photos_pennsylvania = ['")
print(*list(amateur_photos_pennsylvania), sep="',\r\n '")
print("']")
After executing the above, we have a list of amateur photos. Since it must be run locally, here is the list copied in and with newlines inserted, for use by the subsequent learning steps. Note that this list will go stale as Craigslist posts expire, and would need to be refreshed every few weeks.
# https://blacksburg.craigslist.org/search/sss?hasPic=1
# Virginia Craigslist photos:
amateur_photos_virginia = [
'https://images.craigslist.org/00J0J_eNltABqMNjh_0yc0kd_300x300.jpg',
'https://images.craigslist.org/00808_3AVp4eb0ahOz_0iZ0hS_300x300.jpg',
'https://images.craigslist.org/00000_hFkdTLJCApg_0t20CI_300x300.jpg',
'https://images.craigslist.org/00V0V_737IPDzTIXc_0t20CI_300x300.jpg',
'https://images.craigslist.org/00F0F_6FXwhX8nR6V_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00m0m_eLQ0BD0E4nZ_0t20CI_300x300.jpg',
'https://images.craigslist.org/00101_9dGjak78lCu_0t20CI_300x300.jpg',
'https://images.craigslist.org/00d0d_j156UkKlthp_0t20CI_300x300.jpg',
'https://images.craigslist.org/01010_25iCkwdZQTZ_0BF0t2_300x300.jpg',
'https://images.craigslist.org/00S0S_hVgyYCu1QdZ_0t20CI_300x300.jpg',
'https://images.craigslist.org/00z0z_dTeP066NYtc_0t20CI_300x300.jpg',
'https://images.craigslist.org/00505_kiOKla8x5T5_0t20CI_300x300.jpg',
'https://images.craigslist.org/00U0U_gqwRfvUHeJ5_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00U0U_eYU3NulkbVqz_0t20CI_300x300.jpg',
'https://images.craigslist.org/01717_cYwoEmmdfRWz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00L0L_fUBCwD4lHuvz_0iv0t2_300x300.jpg',
'https://images.craigslist.org/00707_dqt587w8YeCz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00l0l_lVmhrsldgygz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00101_2m9gJ6HBa6d_0ww0oo_300x300.jpg',
'https://images.craigslist.org/00G0G_2govN4YFO98_0CI0lM_300x300.jpg',
'https://images.craigslist.org/00i0i_hyQh9AVs7O2_0no0t2_300x300.jpg',
'https://images.craigslist.org/01212_4oA8YUbZMjv_08l06P_300x300.jpg',
'https://images.craigslist.org/00W0W_gwQD25uHvTp_09J07k_300x300.jpg',
'https://images.craigslist.org/00j0j_bst3muvvtQT_0gl0t2_300x300.jpg',
'https://images.craigslist.org/00k0k_6Ya7Vt2orh3_0sy0kZ_300x300.jpg',
'https://images.craigslist.org/00101_5QHY4S2Z5sA_0CI0t2_300x300.jpg',
'https://images.craigslist.org/01515_ffmvYbqaDE5z_0db0il_300x300.jpg',
'https://images.craigslist.org/00Y0Y_k4BLtlmy4G3_0t20CI_300x300.jpg',
'https://images.craigslist.org/00f0f_8UqaoVSYmF3_0t20CI_300x300.jpg',
'https://images.craigslist.org/00S0S_gzUw88HCvAJ_0t20CI_300x300.jpg',
'https://images.craigslist.org/00y0y_33hz0Z55b4q_0t20CI_300x300.jpg',
'https://images.craigslist.org/00303_1C1jCIIZv0j_0vC0sU_300x300.jpg',
'https://images.craigslist.org/00e0e_8CkDtXcKJT8_0t20CI_300x300.jpg',
'https://images.craigslist.org/00y0y_51A8H2vN373_0lM0CI_300x300.jpg',
'https://images.craigslist.org/00L0L_dRk0jnEpyOt_0ak07K_300x300.jpg',
'https://images.craigslist.org/00y0y_4k0av28SZ2D_0t20CI_300x300.jpg',
'https://images.craigslist.org/00q0q_bl8M6uDFhu6_0lN0CI_300x300.jpg',
'https://images.craigslist.org/00s0s_gWI3X0WADJl_0t20CI_300x300.jpg',
'https://images.craigslist.org/00a0a_acXah02uvOWz_0iP0CI_300x300.jpg',
'https://images.craigslist.org/00J0J_8ic6GHco0hWz_0iP0CI_300x300.jpg',
'https://images.craigslist.org/00909_kX0yxtyNj3Kz_0CI0iP_300x300.jpg',
'https://images.craigslist.org/00r0r_aQuKshg2RWz_0CI0m5_300x300.jpg',
'https://images.craigslist.org/00D0D_EXbdo4OwAIz_0CI0iP_300x300.jpg',
'https://images.craigslist.org/00c0c_7XCCCRjC45Xz_0qw0r6_300x300.jpg',
'https://images.craigslist.org/00909_3pVbsQjmG3fz_0iF0t2_300x300.jpg',
'https://images.craigslist.org/00x0x_fDO78qt9EhBz_0kZ0t2_300x300.jpg',
'https://images.craigslist.org/00y0y_zub3q9oOAoz_0gH0t2_300x300.jpg',
'https://images.craigslist.org/00L0L_kEafUzBefJVz_0kn0t2_300x300.jpg',
'https://images.craigslist.org/01515_9YtDTZJoRdN_0sv0t2_300x300.jpg',
'https://images.craigslist.org/00l0l_hxUFSVGPN90_0bC0fu_300x300.jpg',
'https://images.craigslist.org/00000_9Xu5oAHrbD2z_0bC0fu_300x300.jpg',
'https://images.craigslist.org/00606_c9HQzKcHUI5_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00R0R_f11XTZaLsnr_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00l0l_hAeQ6VO5Yqx_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00y0y_kERv1dOnpIW_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00n0n_dE2JSzTa8RV_0t20CI_300x300.jpg',
'https://images.craigslist.org/00606_g7nMdIjUPlJ_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00d0d_aPBp3AfINh1_0CI0pO_300x300.jpg',
'https://images.craigslist.org/00707_lVPnZIWolSD_0t20t2_300x300.jpg',
'https://images.craigslist.org/00L0L_joIASkfRRhf_0lM0t2_300x300.jpg',
'https://images.craigslist.org/00U0U_cwXe5LmtUCd_0CI0lM_300x300.jpg',
'https://images.craigslist.org/00b0b_biVTywYkm10z_0jm09p_300x300.jpg',
'https://images.craigslist.org/00E0E_j8mZhHXmG8Z_0t20CI_300x300.jpg',
'https://images.craigslist.org/01414_buxYPX1YJUj_0lM0t2_300x300.jpg',
'https://images.craigslist.org/00Y0Y_5eD6SeQl6Tb_0fu0bC_300x300.jpg',
'https://images.craigslist.org/00606_brXAhtRTyjH_0t20CI_300x300.jpg',
'https://images.craigslist.org/00S0S_cn6Niv51N8L_0dq0t2_300x300.jpg',
'https://images.craigslist.org/00Y0Y_5eD6SeQl6Tb_0fu0bC_300x300.jpg',
'https://images.craigslist.org/00p0p_2zn1fqnv4URz_0sa0By_300x300.jpg',
'https://images.craigslist.org/00o0o_bgRDCriNfxcz_0fu0kE_300x300.jpg',
'https://images.craigslist.org/00w0w_cnV5ulfFsjKz_0oo0bR_300x300.jpg',
'https://images.craigslist.org/01717_FvloHH4Qzx_0CI0iP_300x300.jpg',
'https://images.craigslist.org/01717_caGn7zohVK9z_0ww0fO_300x300.jpg',
'https://images.craigslist.org/00e0e_lvI5xKTS3wxz_0i40ww_300x300.jpg',
'https://images.craigslist.org/00r0r_cABhiHxo8YSz_0fK0ww_300x300.jpg',
'https://images.craigslist.org/00R0R_gEELQEQo5x4z_0ww0fO_300x300.jpg',
'https://images.craigslist.org/01717_lSJKVNpvnviz_0ww0fO_300x300.jpg',
'https://images.craigslist.org/00606_iwznQHvGoJm_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00G0G_4pUkZMsT3rZz_0ww0fO_300x300.jpg',
'https://images.craigslist.org/00d0d_5WJueKWKEfKz_0ww0fO_300x300.jpg',
'https://images.craigslist.org/00W0W_dcJchrznGvj_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00101_5XUSciY7rmrz_0i40ww_300x300.jpg',
'https://images.craigslist.org/01616_evTksoWGYg_0Cz0t2_300x300.jpg',
'https://images.craigslist.org/00U0U_1fFrJg6sPDZ_0Cz0t2_300x300.jpg',
'https://images.craigslist.org/01616_5xJlvbt7a9b_0hq09O_300x300.jpg',
'https://images.craigslist.org/00d0d_lMWBVtjMnHY_03205a_300x300.jpg',
'https://images.craigslist.org/00404_cC5xefqPNIv_0hq09O_300x300.jpg',
'https://images.craigslist.org/00y0y_7CxaxHsZ3Z1_0lM0t2_300x300.jpg',
'https://images.craigslist.org/00O0O_1s0gzObVAGz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00Z0Z_lHdcJV1BLSMz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00p0p_cdJGQJTda6hz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00M0M_eXf4KqmgYXWz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00G0G_4WKmfvyXLk7z_0i40ww_300x300.jpg',
'https://images.craigslist.org/00C0C_3QnxkcOlnvnz_0jL0ob_300x300.jpg',
'https://images.craigslist.org/00F0F_7EmU5PvBKx7_0CI0oc_300x300.jpg',
'https://images.craigslist.org/00U0U_huBmSyQdZk2_0CI0it_300x300.jpg',
'https://images.craigslist.org/00g0g_7gkDQ2IQDrH_0CI0vp_300x300.jpg',
'https://images.craigslist.org/00y0y_20B1d7AKI7f_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00707_27Lc3wFqGYh_0t20CI_300x300.jpg',
'https://images.craigslist.org/00y0y_20B1d7AKI7f_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00p0p_i9BsLDhipKS_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00808_83OfVZISXam_0C30t2_300x300.jpg',
'https://images.craigslist.org/00p0p_i9BsLDhipKS_0CI0t2_300x300.jpg',
'https://images.craigslist.org/01313_hUr5sxkEZuj_0t20CI_300x300.jpg',
'https://images.craigslist.org/00k0k_fV6KDePgio6z_0t20CI_300x300.jpg',
'https://images.craigslist.org/00j0j_do8Cs7sGsRGz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00F0F_4b4TiyN1PgL_0t20CI_300x300.jpg',
'https://images.craigslist.org/00t0t_69fXsouHG1v_0t20CI_300x300.jpg',
'https://images.craigslist.org/00606_1fJBCuOgT9C_0t20CI_300x300.jpg',
'https://images.craigslist.org/00T0T_fy3UCafvBwX_0jm0pO_300x300.jpg',
'https://images.craigslist.org/00606_1fJBCuOgT9C_0t20CI_300x300.jpg',
'https://images.craigslist.org/00d0d_5WJueKWKEfKz_0ww0fO_300x300.jpg',
'https://images.craigslist.org/00T0T_2UaFGLoJ0f8_0CI0og_300x300.jpg',
'https://images.craigslist.org/00d0d_87kvRy9Q8X8_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00000_ixg1Jdb1FlV_0CI0nH_300x300.jpg',
'https://images.craigslist.org/00a0a_jqLQr5pstgq_0oA0fN_300x300.jpg',
'https://images.craigslist.org/00x0x_31bOHY9TQsh_0t20CI_300x300.jpg',
'https://images.craigslist.org/00A0A_681SYGdOqhn_0hq0CI_300x300.jpg',
'https://images.craigslist.org/00Z0Z_ajif0DJAxP1_0g80g8_300x300.jpg',
'https://images.craigslist.org/00K0K_dzHgAqMrGLGz_04Q03V_300x300.jpg'
]
# https://corvallis.craigslist.org/search/sss?hasPic=1
# Oregon Craigslist photos:
amateur_photos_oregon = [
'https://images.craigslist.org/00p0p_fVxdWVxjJVb_0jm0aT_300x300.jpg',
'https://images.craigslist.org/00t0t_3uGqyoVGbmW_0lM0t2_300x300.jpg',
'https://images.craigslist.org/00b0b_elegFK6kgIm_0tR0CI_300x300.jpg',
'https://images.craigslist.org/00a0a_iE7xwWgIgIQ_0t20CI_300x300.jpg',
'https://images.craigslist.org/00r0r_1y6ijBHvDTH_0t20CI_300x300.jpg',
'https://images.craigslist.org/00B0B_8AoXANOxiFR_0sB0t2_300x300.jpg',
'https://images.craigslist.org/01414_46u2lZcGKFp_07e05K_300x300.jpg',
'https://images.craigslist.org/01717_jUPn0bjRssX_0CI0pE_300x300.jpg',
'https://images.craigslist.org/00000_bYxMxn4YnWK_0Ai0t2_300x300.jpg',
'https://images.craigslist.org/01313_2L7BixrXlcf_0il0CI_300x300.jpg',
'https://images.craigslist.org/00i0i_i0Riye8qDij_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00Z0Z_3kLVZ8Chlhh_0t20CI_300x300.jpg',
'https://images.craigslist.org/00R0R_7G6aBbKn2O2_0t20CI_300x300.jpg',
'https://images.craigslist.org/01313_2L7BixrXlcf_0il0CI_300x300.jpg',
'https://images.craigslist.org/00B0B_lTvkrFUTUWbz_0CI0bW_300x300.jpg',
'https://images.craigslist.org/00606_doUiFlSfqmHz_0x20oM_300x300.jpg',
'https://images.craigslist.org/00b0b_afARmDcC0PU_0t20CI_300x300.jpg',
'https://images.craigslist.org/00J0J_lnVMGiHhSdw_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00L0L_hp6YY3VDzMqz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00E0E_gTg80g0iWDjz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00V0V_3kvwYpV48lW_0CI0t2_300x300.jpg',
'https://images.craigslist.org/01414_8Hai7RT1DJmz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00n0n_2b0oyVitXEGz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00n0n_5Lx9gU6QUSiz_0lM0t2_300x300.jpg',
'https://images.craigslist.org/00K0K_2IuSY6CyMN7z_0lM0t2_300x300.jpg',
'https://images.craigslist.org/00808_hAmF0SA84D7z_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00L0L_1wgAGViZMmV_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00000_eOXpZyWj7Hnz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00q0q_3aojkmvdWtWz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00n0n_7uvxXBv83q6z_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00o0o_f27C1vADBxNz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00O0O_dypjbnyFuDcz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00Z0Z_84WFbPjbyhkz_0da09S_300x300.jpg',
'https://images.craigslist.org/00c0c_hssP62JAqJ1z_0lM0t2_300x300.jpg',
'https://images.craigslist.org/00Y0Y_cYTH4ImUarSz_0fu0bC_300x300.jpg',
'https://images.craigslist.org/00v0v_ftRAZKqf7QNz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00C0C_9n5Qvx6YrS5z_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00a0a_kRcOFbjzcjwz_0hQ0CI_300x300.jpg',
'https://images.craigslist.org/01414_hl2TnrQfbe4_0gG0t2_300x300.jpg',
'https://images.craigslist.org/00Z0Z_2krTFwRtwPq_0lM0CI_300x300.jpg',
'https://images.craigslist.org/00i0i_8h1tSh5am92_0t20CI_300x300.jpg',
'https://images.craigslist.org/00Y0Y_X6IYDfBvwm_0lM0CI_300x300.jpg',
'https://images.craigslist.org/00D0D_kbYUiEdLx2p_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00606_dUwX2xdWOY4_0t20CI_300x300.jpg',
'https://images.craigslist.org/00C0C_jhCTCuwoL0yz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00505_YLr5Wiru3S_0lM0CI_300x300.jpg',
'https://images.craigslist.org/00606_kzeT6n1sNy4_0t20CI_300x300.jpg',
'https://images.craigslist.org/01111_cnnSt51wyw1z_076076_300x300.jpg',
'https://images.craigslist.org/00G0G_7gf5wDl26fXz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00k0k_9YGj74lPOGf_0lM0t2_300x300.jpg',
'https://images.craigslist.org/00v0v_anU9V0rcxN1z_0gl0t2_300x300.jpg',
'https://images.craigslist.org/00b0b_6rj1rxnmcP3_0lM0CI_300x300.jpg',
'https://images.craigslist.org/00404_7l4xPkzv6ga_0lM0CI_300x300.jpg',
'https://images.craigslist.org/00T0T_cV6C1NWVTt_0t20CI_300x300.jpg',
'https://images.craigslist.org/00F0F_hgYKX0qwuQo_0lM0CI_300x300.jpg',
'https://images.craigslist.org/00909_54WiNDsP4gR_0th0CI_300x300.jpg',
'https://images.craigslist.org/00q0q_2OtYoEXCDLwz_09G06s_300x300.jpg',
'https://images.craigslist.org/00A0A_aoTM3UmKdqS_1320MM_300x300.jpg',
'https://images.craigslist.org/00L0L_g8scaYvXt3T_0t20CI_300x300.jpg',
'https://images.craigslist.org/00l0l_7ImFuDhX661z_0mh0t2_300x300.jpg',
'https://images.craigslist.org/00o0o_lMHeyeh8g2b_0jm0aT_300x300.jpg',
'https://images.craigslist.org/00e0e_1P8UbJFuDxK_1320MM_300x300.jpg',
'https://images.craigslist.org/01010_kcEPll19hhC_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00202_6ueAPFx6bk4z_0910ew_300x300.jpg',
'https://images.craigslist.org/00N0N_fnaFlq5MxLt_0t20CI_300x300.jpg',
'https://images.craigslist.org/00s0s_8fi1vod1tYwz_09G07g_300x300.jpg',
'https://images.craigslist.org/00Y0Y_11DAxNoBLvF_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00W0W_kP02d2u4piD_0t20CI_300x300.jpg',
'https://images.craigslist.org/00808_gH51sNf4lVJ_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00n0n_93rVz9sAcjg_0t20CI_300x300.jpg',
'https://images.craigslist.org/00l0l_7bm41LQZizz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00R0R_4NMLXtLZIb9z_0t20CI_300x300.jpg',
'https://images.craigslist.org/00R0R_4NMLXtLZIb9z_0t20CI_300x300.jpg',
'https://images.craigslist.org/00L0L_fpeCvGTrxqU_0t20CI_300x300.jpg',
'https://images.craigslist.org/00e0e_4OkfeN6lusm_0d40t2_300x300.jpg',
'https://images.craigslist.org/00M0M_4KIMK2lxEnj_0t20CI_300x300.jpg',
'https://images.craigslist.org/00j0j_iRlSOUzGewD_0hR0CI_300x300.jpg',
'https://images.craigslist.org/00O0O_eql7f7FUIU6z_0pp0t2_300x300.jpg',
'https://images.craigslist.org/00E0E_gQEksD31WIL_0lM0CI_300x300.jpg',
'https://images.craigslist.org/00V0V_iBbhzvrWdYOz_0aT0ew_300x300.jpg',
'https://images.craigslist.org/00707_1uuF37GVUiz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00o0o_clr0Db0AInJ_07v0a0_300x300.jpg',
'https://images.craigslist.org/00D0D_7ovR9JRViVb_0t20CI_300x300.jpg',
'https://images.craigslist.org/01010_b3mCUirtFBJ_0jm0jm_300x300.jpg',
'https://images.craigslist.org/00t0t_6KZqrc5cqw0_0pO0jm_300x300.jpg',
'https://images.craigslist.org/00U0U_gwsqwXwmDZr_0CI0iP_300x300.jpg',
'https://images.craigslist.org/00X0X_hKWYLtk0CbC_0t20CI_300x300.jpg',
'https://images.craigslist.org/00u0u_1SFApp7DEt2z_0aT0ew_300x300.jpg',
'https://images.craigslist.org/00N0N_4xa8nbCcpd2_0CI0t2_300x300.jpg',
'https://images.craigslist.org/01717_69MOM9JNwYvz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00505_gTa5PA0x3Enz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00g0g_9hBrdam6G67_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00B0B_8N4zrjHMWKc_0t20CI_300x300.jpg',
'https://images.craigslist.org/00m0m_1SFApp7DEt2_0aT0ew_300x300.jpg',
'https://images.craigslist.org/00v0v_3N1l7vfP9oX_0t20CI_300x300.jpg',
'https://images.craigslist.org/00E0E_i7w9xzQ4DVnz_0Cr0t2_300x300.jpg',
'https://images.craigslist.org/00C0C_9Pme5Q5Fab4z_0830cU_300x300.jpg',
'https://images.craigslist.org/00C0C_9Pme5Q5Fab4z_0830cU_300x300.jpg',
'https://images.craigslist.org/00U0U_2ImDoMOrJg1_0lM0t2_300x300.jpg',
'https://images.craigslist.org/00S0S_kgHOeKxpbE0_0t20CI_300x300.jpg',
'https://images.craigslist.org/00J0J_gcavERpafL5_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00Q0Q_fn6dOVjOrhq_05O07K_300x300.jpg',
'https://images.craigslist.org/00h0h_gNXXK78iUzB_0k10t2_300x300.jpg',
'https://images.craigslist.org/00b0b_4JxBUlc1eDT_07K0ak_300x300.jpg',
'https://images.craigslist.org/00V0V_ks1vocYQacpz_0fu0bC_300x300.jpg',
'https://images.craigslist.org/01010_fGeu0ggYIE1z_0fu0bC_300x300.jpg',
'https://images.craigslist.org/00N0N_9Kv90TDMVQmz_0fu0bC_300x300.jpg',
'https://images.craigslist.org/01616_6gtu5DweqMK_0t20CI_300x300.jpg',
'https://images.craigslist.org/00t0t_jiuBppu6cah_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00D0D_awLxMdri04H_0gl0t2_300x300.jpg',
'https://images.craigslist.org/00L0L_lMpIOyA7a5P_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00J0J_34dt5RdFZGL_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00w0w_3lBvw0Wz9ba_0t20CI_300x300.jpg',
'https://images.craigslist.org/00u0u_75151uOteJ3_0t20CI_300x300.jpg',
'https://images.craigslist.org/00X0X_kkfRyb2oCcc_0ak07K_300x300.jpg',
'https://images.craigslist.org/01616_fiBrckaM2EF_0t20CI_300x300.jpg',
'https://images.craigslist.org/00Z0Z_djGI9GIn02H_07K0ak_300x300.jpg',
'https://images.craigslist.org/00808_4JusbeQ1pY3_0t20CI_300x300.jpg',
'https://images.craigslist.org/00X0X_kkfRyb2oCcc_0ak07K_300x300.jpg',
'https://images.craigslist.org/00H0H_joPQWU3gAR7_07K0ak_300x300.jpg'
]
# Pennsylvania Craigslist photos:
amateur_photos_pennsylvania = [
'https://images.craigslist.org/00Z0Z_HhMkxBontE_0g80g8_300x300.jpg',
'https://images.craigslist.org/00202_l1Lb4mg6euW_0g40c8_300x300.jpg',
'https://images.craigslist.org/00p0p_jZA5ydo5i64_0t20CI_300x300.jpg',
'https://images.craigslist.org/00u0u_bDRM4NX3f93z_0hq0CI_300x300.jpg',
'https://images.craigslist.org/00V0V_jF9bzJcO8N0_0ak07K_300x300.jpg',
'https://images.craigslist.org/00d0d_94T9sVbEurTz_0lj0t2_300x300.jpg',
'https://images.craigslist.org/00l0l_kmYH924pYno_0t20CI_300x300.jpg',
'https://images.craigslist.org/00A0A_g9nZYWwCvuE_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00y0y_boXiLGz1BMV_0t20CI_300x300.jpg',
'https://images.craigslist.org/00t0t_9Gd8CP1vVGO_0t20CI_300x300.jpg',
'https://images.craigslist.org/00x0x_9Z9K1uIG6dK_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00000_2EOVFflaR5G_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00b0b_1tlTvrbvDSkz_04B04G_300x300.jpg',
'https://images.craigslist.org/00v0v_7K66cOSxyBjz_0CI0n8_300x300.jpg',
'https://images.craigslist.org/00b0b_1tlTvrbvDSkz_04B04G_300x300.jpg',
'https://images.craigslist.org/00606_pDV8R3FgdU_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00707_hDjm5yQzMF5_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00707_kSIDH6ylX9Oz_0od0t2_300x300.jpg',
'https://images.craigslist.org/01212_7R2lYNauwOKz_0cR0e9_300x300.jpg',
'https://images.craigslist.org/00j0j_72x6knlmWkAz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00U0U_9clNdBGuen5_0lR0t2_300x300.jpg',
'https://images.craigslist.org/00F0F_8YYYY5qAine_0p60t2_300x300.jpg',
'https://images.craigslist.org/00h0h_adhi4UI4eGa_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00N0N_7dzRvUSgGUVz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00R0R_9nY8B7q0hCe_0t20CI_300x300.jpg',
'https://images.craigslist.org/00J0J_3zf5o9j2YgH_0t20CI_300x300.jpg',
'https://images.craigslist.org/00909_blZhXYfcMp4_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00303_ehYaQiDjpsU_0jm0pO_300x300.jpg',
'https://images.craigslist.org/00D0D_56PcLACIWRSz_0rO0kR_300x300.jpg',
'https://images.craigslist.org/00303_giy9IIEHMxt_0CI0t2_300x300.jpg',
'https://images.craigslist.org/01515_7EahhRC6sHC_0dq0t2_300x300.jpg',
'https://images.craigslist.org/00z0z_fSa1MjFqxpx_0aT0ew_300x300.jpg',
'https://images.craigslist.org/00g0g_leO695exVJ1z_0ww0oo_300x300.jpg',
'https://images.craigslist.org/01616_k4ERTKSbGmLz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00q0q_ifXWR5u7MHCz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00G0G_34P9QjDQRBCz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00F0F_47gtkWdGjcxz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/01515_bZs0QMIMtCQz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00K0K_3ivAKOqPSAt_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00B0B_iVCpbZGHEfZ_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00G0G_50owFT81UxK_0t20CI_300x300.jpg',
'https://images.craigslist.org/00T0T_gYvcqGpdSSe_0lM0t2_300x300.jpg',
'https://images.craigslist.org/00P0P_fbvYRoh3iuu_0ew0t2_300x300.jpg',
'https://images.craigslist.org/00H0H_i7LBBZdFFQU_0hq0d4_300x300.jpg',
'https://images.craigslist.org/00n0n_lQ2zqFWGlLC_0hq0ne_300x300.jpg',
'https://images.craigslist.org/00x0x_jiybf4mjPeS_0d40hq_300x300.jpg',
'https://images.craigslist.org/00J0J_8L4EMo9YT30_0hq0d4_300x300.jpg',
'https://images.craigslist.org/00t0t_Kfv5R7pTx1_0jI0eM_300x300.jpg',
'https://images.craigslist.org/00N0N_3l6iblTQGLh_0hq0d4_300x300.jpg',
'https://images.craigslist.org/00202_ksD3EAsCdE6_0d40hq_300x300.jpg',
'https://images.craigslist.org/01515_2Zp0ivI6yOa_0hq0d4_300x300.jpg',
'https://images.craigslist.org/00l0l_lvrkqQn7Zhh_0hq0d4_300x300.jpg',
'https://images.craigslist.org/00H0H_6wNjQUW6DVk_0iE0dZ_300x300.jpg',
'https://images.craigslist.org/00I0I_2w0Agximwsj_0CI0t2_300x300.jpg',
'https://images.craigslist.org/01010_8YSvcBSIVIy_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00k0k_UhFc0dfudG_0hT0CI_300x300.jpg',
'https://images.craigslist.org/00k0k_UhFc0dfudG_0hT0CI_300x300.jpg',
'https://images.craigslist.org/01717_cCTRrv9CctG_0t20CI_300x300.jpg',
'https://images.craigslist.org/01717_hsjgWvdvfsr_0aT0ew_300x300.jpg',
'https://images.craigslist.org/00b0b_bUwP2qykNUOz_0dW0iA_300x300.jpg',
'https://images.craigslist.org/00f0f_iaHXdSLZOWvz_06s04w_300x300.jpg',
'https://images.craigslist.org/00x0x_boaXrVxrUS2_0lM0CI_300x300.jpg',
'https://images.craigslist.org/00s0s_1HhF7CfZB6o_0t20CI_300x300.jpg',
'https://images.craigslist.org/00P0P_bG9V3TBID6yz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00B0B_8wYNrAhHpen_0yp0t2_300x300.jpg',
'https://images.craigslist.org/00K0K_b8YnhjBGzAuz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00l0l_3GCt6eXzWa6_0t20CI_300x300.jpg',
'https://images.craigslist.org/00l0l_gZm6O1lRmCm_0pO0jm_300x300.jpg',
'https://images.craigslist.org/00g0g_fpZ8TyOr66F_1320MM_300x300.jpg',
'https://images.craigslist.org/00P0P_ePZxFtw3h19_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00r0r_6dzEtWC3EIR_0t20CI_300x300.jpg',
'https://images.craigslist.org/00Z0Z_8lyB0GTGN5R_0t20CI_300x300.jpg',
'https://images.craigslist.org/00909_lsVk82VfTFm_08B06s_300x300.jpg',
'https://images.craigslist.org/00f0f_hkAJ16hkJDiz_06s08B_300x300.jpg',
'https://images.craigslist.org/00n0n_d7AMxacdeahz_06s08B_300x300.jpg',
'https://images.craigslist.org/01212_gI46z4XvrJkz_06s04Q_300x300.jpg',
'https://images.craigslist.org/00f0f_975Q7TCenNgz_06s04Q_300x300.jpg',
'https://images.craigslist.org/00d0d_bCIZ45yD5g7z_06s08B_300x300.jpg',
'https://images.craigslist.org/01313_5Mi0b4EJ8tQz_06s08B_300x300.jpg',
'https://images.craigslist.org/00c0c_alBwUQaEzogz_06s08B_300x300.jpg',
'https://images.craigslist.org/00m0m_76TYVr0Rt91z_06s04Q_300x300.jpg',
'https://images.craigslist.org/00w0w_51QPag7mf90_06s08B_300x300.jpg',
'https://images.craigslist.org/00N0N_7Pc3864Rtsz_06s08B_300x300.jpg',
'https://images.craigslist.org/00s0s_ecMJA0RLJb1z_06s08B_300x300.jpg',
'https://images.craigslist.org/00r0r_cHFSy1cdeft_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00d0d_98EaTfXbyzk_0t20CI_300x300.jpg',
'https://images.craigslist.org/01616_8WGZwoPtjUZz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00A0A_b7IYLjReeye_01W02A_300x300.jpg',
'https://images.craigslist.org/00202_l1V63yiL19n_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00S0S_989GUflKxKa_0t20CI_300x300.jpg',
'https://images.craigslist.org/01616_ecGN1eTeDtoz_09G07g_300x300.jpg',
'https://images.craigslist.org/00303_gjDMFV8M2Wb_04Q04M_300x300.jpg',
'https://images.craigslist.org/00Y0Y_7lLGrscreyF_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00909_6M5rycwCdGO_0t20CI_300x300.jpg',
'https://images.craigslist.org/01212_hmnm5PwL0ei_04a0aP_300x300.jpg',
'https://images.craigslist.org/00G0G_czrGkcpC4Fo_0jm0pO_300x300.jpg',
'https://images.craigslist.org/01313_cr2pTOd7tLn_1320MM_300x300.jpg',
'https://images.craigslist.org/00a0a_kneu5TDsG6V_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00O0O_d3TQgeGCHtF_0MM132_300x300.jpg',
'https://images.craigslist.org/00w0w_2GyL7yoL9yc_1320MM_300x300.jpg',
'https://images.craigslist.org/00x0x_iCC7Xmz8DHo_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00n0n_3W9aI6DwSOg_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00O0O_kOvC4I7bWNB_0t20CI_300x300.jpg',
'https://images.craigslist.org/01111_btuFx0k1J9Y_0ak0ak_300x300.jpg',
'https://images.craigslist.org/00L0L_dOl4pJEglD8_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00q0q_c2RjsXmfi2z_1320MM_300x300.jpg',
'https://images.craigslist.org/00y0y_ga2xbqY0joSz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/01616_5N3wXM776vMz_0t20CI_300x300.jpg',
'https://images.craigslist.org/00J0J_ivHjq6GI5Ud_0gw0co_300x300.jpg',
'https://images.craigslist.org/00w0w_6MwyZY6JQzf_0t20CI_300x300.jpg',
'https://images.craigslist.org/00n0n_stuPEoMp33z_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00Q0Q_bd7L7YSJwaGz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00C0C_erzPThEJyaJz_0lM0t2_300x300.jpg',
'https://images.craigslist.org/00303_8itPxqhI469z_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00202_bD6QXeYiIHLz_0jm0pO_300x300.jpg',
'https://images.craigslist.org/00m0m_erTjg7WLro5z_09G06X_300x300.jpg',
'https://images.craigslist.org/00c0c_aa0d1u0gZaMz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00909_3w51bweIwWxz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00X0X_br0eRSjKoAEz_0CI0t2_300x300.jpg',
'https://images.craigslist.org/00p0p_9RL0I7Xgdbaz_0CI0t2_300x300.jpg'
]
The rest of this notebook is taken mostly verbatim from the first exercise from fast.ai, found here:
https://www.kaggle.com/code/jhoward/is-it-a-bird-creating-a-model-from-your-own-data
These steps would be run online to take advantage of server resources, and not needing to download and setup the sizeable fast.ai packages.
#NB: Kaggle requires phone verification to use the internet or a GPU. If you haven't done that yet, the cell below will fail
# This code is only here to check that your internet is enabled. It doesn't do anything else.
# Here's a help thread on getting your phone number verified: https://www.kaggle.com/product-feedback/135367
import socket,warnings
try:
socket.setdefaulttimeout(1)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(('1.1.1.1', 53))
except socket.error as ex: raise Exception("STOP: No internet. Click '>|' in top right and set 'Internet' switch to on")
# It's a good idea to ensure you're running the latest version of any libraries you need.
# `!pip install -Uqq <libraries>` upgrades to the latest version of <libraries>
# NB: You can safely ignore any warnings or errors pip spits out about running as root or incompatibilities
import os
iskaggle = os.environ.get('KAGGLE_KERNEL_RUN_TYPE', '')
if iskaggle:
!pip install -Uqq fastai duckduckgo_search
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorflow-io 0.21.0 requires tensorflow-io-gcs-filesystem==0.21.0, which is not installed.
explainable-ai-sdk 1.3.2 requires xai-image-widget, which is not installed.
dask-cudf 21.10.1 requires cupy-cuda114, which is not installed.
beatrix-jupyterlab 3.1.6 requires google-cloud-bigquery-storage, which is not installed.
tensorflow 2.6.2 requires numpy~=1.19.2, but you have numpy 1.20.3 which is incompatible.
tensorflow 2.6.2 requires six~=1.15.0, but you have six 1.16.0 which is incompatible.
tensorflow 2.6.2 requires typing-extensions~=3.7.4, but you have typing-extensions 3.10.0.2 which is incompatible.
tensorflow 2.6.2 requires wrapt~=1.12.1, but you have wrapt 1.13.3 which is incompatible.
tensorflow-transform 1.5.0 requires absl-py<0.13,>=0.9, but you have absl-py 0.15.0 which is incompatible.
tensorflow-transform 1.5.0 requires numpy<1.20,>=1.16, but you have numpy 1.20.3 which is incompatible.
tensorflow-transform 1.5.0 requires pyarrow<6,>=1, but you have pyarrow 6.0.1 which is incompatible.
tensorflow-transform 1.5.0 requires tensorflow!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,<2.8,>=1.15.2, but you have tensorflow 2.6.2 which is incompatible.
tensorflow-serving-api 2.7.0 requires tensorflow<3,>=2.7.0, but you have tensorflow 2.6.2 which is incompatible.
gcsfs 2021.11.1 requires fsspec==2021.11.1, but you have fsspec 2022.2.0 which is incompatible.
flake8 4.0.1 requires importlib-metadata<4.3; python_version < "3.8", but you have importlib-metadata 4.11.3 which is incompatible.
featuretools 1.6.0 requires numpy>=1.21.0, but you have numpy 1.20.3 which is incompatible.
dask-cudf 21.10.1 requires dask==2021.09.1, but you have dask 2022.2.0 which is incompatible.
dask-cudf 21.10.1 requires distributed==2021.09.1, but you have distributed 2022.2.0 which is incompatible.
apache-beam 2.34.0 requires dill<0.3.2,>=0.3.1.1, but you have dill 0.3.4 which is incompatible.
apache-beam 2.34.0 requires httplib2<0.20.0,>=0.8, but you have httplib2 0.20.2 which is incompatible.
apache-beam 2.34.0 requires pyarrow<6.0.0,>=0.15.1, but you have pyarrow 6.0.1 which is incompatible.
aioitertools 0.10.0 requires typing_extensions>=4.0; python_version < "3.10", but you have typing-extensions 3.10.0.2 which is incompatible.
aiobotocore 2.1.2 requires botocore<1.23.25,>=1.23.24, but you have botocore 1.24.20 which is incompatible.
Step 1: Download images of stock photography
from duckduckgo_search import ddg_images
from fastcore.all import *
def search_images(term, max_images):
print(f"Searching for '{term}'")
return L(ddg_images(term, max_results=max_images)).itemgot('image')
#NB: `search_images` depends on duckduckgo.com, which doesn't always return correct responses.
# If you get a JSON error, just try running it again (it may take a couple of tries).
urls = search_images('pixabay product', max_images=10)
#urls = search_images('bird photos', max_images=1)
#urls = search_images('stock photos product', max_images=1)
#urls = search_images('shutterstock product', max_images=1)
urls[5]
Searching for 'pixabay product'
'https://cdn.pixabay.com/photo/2018/08/16/11/14/product-3610174_1280.jpg'
from fastdownload import download_url
dest = 'stock_product_photo.jpg'
download_url(urls[0], dest, show_progress=False)
from fastai.vision.all import *
im = Image.open(dest)
im.to_thumb(256,256)
download_url(amateur_photos_virginia[0], 'amateur_product_photo.jpg', show_progress=False)
Image.open('amateur_product_photo.jpg').to_thumb(256,256)
categories = 'stock','amateur'
path = Path('stock_or_amateur')
from time import sleep
s = 'stock'
dest_s = (path/s)
# Clean up from prior run if needed
if os.path.isdir(dest_s):
shutil.rmtree(dest_s)
dest_s.mkdir(exist_ok=True, parents=True)
stock_image_urls_product=search_images('pixabay product', 120)
download_images(dest_s, urls=stock_image_urls_product)
print(f"Number of stock product image URLs: {len(stock_image_urls_product)}")
sleep(8) # Pause between searches to avoid over-loading server
stock_image_urls_business=search_images('pixabay business', 120)
download_images(dest_s, urls=stock_image_urls_business)
print(f"Number of stock business image URLs: {len(stock_image_urls_business)}")
sleep(8)
stock_image_urls_industrial=search_images('pixabay industrial', 120)
download_images(dest_s, urls=stock_image_urls_industrial)
print(f"Number of stock industrial image URLs: {len(stock_image_urls_industrial)}")
resize_images(dest_s, max_size=400, dest=dest_s)
print(f"Total stock images downloaded: {len(get_image_files(dest_s))}")
Searching for 'pixabay product'
Number of stock product image URLs: 120
Searching for 'pixabay business'
Number of stock business image URLs: 120
Searching for 'pixabay industrial'
Number of stock industrial image URLs: 120
/opt/conda/lib/python3.7/site-packages/PIL/Image.py:963: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images
"Palette images with Transparency expressed in bytes should be "
Total stock images downloaded: 354
a = 'amateur'
dest_a = (path/a)
# Clean up from prior run if needed
if os.path.isdir(dest_a):
shutil.rmtree(dest_a)
dest_a.mkdir(exist_ok=True, parents=True)
download_images(dest_a, urls=amateur_photos_virginia) # hard coded above, may need to refresh
print(f"Number of amateur Virginia image URLs: {len(amateur_photos_virginia)}")
sleep(8)
download_images(dest_a, urls=amateur_photos_oregon) # hard coded above, may need to refresh
print(f"Number of amateur Oregon image URLs: {len(amateur_photos_oregon)}")
sleep(8)
download_images(dest_a, urls=amateur_photos_pennsylvania) # hard coded above, may need to refresh
print(f"Number of amateur Pennsylvania image URLs: {len(amateur_photos_pennsylvania)}")
resize_images(dest_a, max_size=400, dest=dest_a)
print(f"Total amateur images downloaded: {len(get_image_files(dest_a))}")
Number of amateur Virginia image URLs: 120
Number of amateur Oregon image URLs: 120
Number of amateur Pennsylvania image URLs: 120
Total amateur images downloaded: 360
Step 2: Train our model
Some photos might not download correctly which could cause our model training to fail, so we'll remove them:
failed = verify_images(get_image_files(path))
failed.map(Path.unlink)
print(f"Failed images removed: {len(failed)}")
total_num_images = len(get_image_files(path))
print(f"Total images after removal: {total_num_images}")
Failed images removed: 0
Total images after removal: 714
To train a model, we'll need DataLoaders, which is an object that contains a training set (the images used to create a model) and a validation set (the images used to check the accuracy of a model -- not used during training). In fastai we can create that easily using a DataBlock, and view sample images from it:
dls = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2, seed=42),
get_y=parent_label,
item_tfms=[Resize(192, method='squish')]
).dataloaders(path, bs=32)
dls.show_batch(max_n=6)
Here what each of the DataBlock parameters means:
blocks=(ImageBlock, CategoryBlock),
The inputs to our model are images, and the outputs are categories (in this case, "bird" or "forest").
get_items=get_image_files,
To find all the inputs to our model, run the get_image_files function (which returns a list of all image files in a path).
splitter=RandomSplitter(valid_pct=0.2, seed=42),
Split the data into training and validation sets randomly, using 20% of the data for the validation set.
get_y=parent_label,
The labels (y values) is the name of the parent of each file (i.e. the name of the folder they're in, which will be bird or forest).
item_tfms=[Resize(192, method='squish')]
Before training, resize each image to 192x192 pixels by "squishing" it (as opposed to cropping it).
Now we're ready to train our model. The fastest widely used computer vision model is resnet18. You can train this in a few minutes, even on a CPU! (On a GPU, it generally takes under 10 seconds...)
fastai comes with a helpful fine_tune() method which automatically uses best practices for fine tuning a pre-trained model, so we'll use that.
learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(3)
Downloading: "https://download.pytorch.org/models/resnet18-f37072fd.pth" to /root/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth
0%| | 0.00/44.7M [00:00<?, ?B/s]
epoch train_loss valid_loss error_rate time
0 0.918096 0.318988 0.126761 00:09
epoch train_loss valid_loss error_rate time
0 0.355654 0.180829 0.077465 00:04
1 0.236539 0.240909 0.077465 00:03
2 0.159012 0.190749 0.063380 00:03
Generally when I run this I see 100% accuracy on the validation set (although it might vary a bit from run to run).
"Fine-tuning" a model means that we're starting with a model someone else has trained using some other dataset (called the pretrained model), and adjusting the weights a little bit so that the model learns to recognise your particular dataset. In this case, the pretrained model was trained to recognise photos in imagenet, and widely-used computer vision dataset with images covering 1000 categories) For details on fine-tuning and why it's important, check out the free fast.ai course.
Step 3: Use our model (and build your own!)
Let's see what our model thinks about that bird we downloaded at the start:
# original:
#is_amateur_photo,_,probs = learn.predict(PILImage.create('stock_product_photo.jpg'))
# it seems as of the Feb 14th 2023 release of fastai 2.7.11 this causes an "attribute read error", eventually found that wrapping in tensor() fixed it:
is_amateur_photo,_,probs = learn.predict(tensor(PILImage.create('stock_product_photo.jpg')))
print(f"This is a: {is_amateur_photo} photo.")
print(f"Probability it's an amateur photo: {probs[0]:.4f}")
This is a: stock photo.
Probability it's an amateur photo: 0.0007
# Pick a random image from the training set to display.
# (This should instead be one not from the training set, but this is just a quick test.)
import random
index = random.randint(0,total_num_images-1)
print(f"index: {index}")
random_image = get_image_files(path)[index]
photo_prediction,_,probs = learn.predict(tensor(PILImage.create(random_image)))
print(f"Prediction: {photo_prediction} photo")
print(f"Probability it's an amateur photo: {probs[0]:.4f}")
print(f"Probability it's a stock photo: {1.0-probs[0]:.4f}")
print(random_image)
Image.open(random_image).to_thumb(256,256)
index: 441
Prediction: stock photo
Probability it's an amateur photo: 0.0003
Probability it's a stock photo: 0.9997
stock_or_amateur/stock/24115c7c-4e28-4709-8472-63d8f6ef9a4e.jpg
Step 4: Analyze model
Now adding analysis and cleaning from lesson 2: https://github.com/fastai/fastbook/blob/master/02_production.ipynb
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
interp.plot_top_losses(6, nrows=1)
#hide
! [ -e /content ] && pip install -Uqq fastbook
import fastbook
fastbook.setup_book()
#hide
from fastbook import *
from fastai.vision.widgets import *
#hide_output
cleaner = ImageClassifierCleaner(learn)
cleaner
Step 5: Export the trained model
learn.export('model.pkl')