Looking at your code it looks like the way you have used resize_images
method looks kind of wrong to me, as you are resizing to same folder. In Jeremy’s notebook in Paddy competition, I have seen him resizing like the below code:
trn_path = Path('sml')
resize_images(path/'train_images', dest=trn_path, max_size=256, recurse=True)
I have tweaked the code similarly and it’s running as expected when I tried running it as a python script.
from fastcore.all import *
from fastai.vision.all import *
from fastdownload import download_url
import time
def search_images(term, max_images=200):
url = "https://duckduckgo.com/"
res = urlread(url, data={"q": term})
searchObj = re.search(r"vqd=([\d-]+)\&", res)
requestUrl = url + "i.js"
params = dict(
l="us-en", o="json", q=term, vqd=searchObj.group(1), f=",,,", p="1", v7exp="a"
)
urls, data = set(), {"next": 1}
while len(urls) < max_images and "next" in data:
data = urljson(requestUrl, data=params)
urls.update(L(data["results"]).itemgot("image"))
requestUrl = url + data["next"]
time.sleep(0.2)
return L(urls)[:max_images]
urls = search_images("photos of sardines", max_images=1)
urls[0]
dest = "sardines.jpg"
download_url(urls[0], dest, show_progress=False)
im = Image.open(dest)
im.to_thumb(256, 256)
download_url(
search_images("photos of cod fish", max_images=1)[0],
"codfish.jpg",
show_progress=False,
)
searches = "codfish", "sardines"
path = Path("sardine_or_not")
resize_path = Path("resized")
# print searches
for o in searches:
dest = path / o
dest.mkdir(exist_ok=True, parents=True)
download_images(dest, urls=search_images(f"photos of {o}"))
resize_images(path / o, max_size=400, dest=resize_path / o)
failed = verify_images(get_image_files(path))
failed.map(Path.unlink)
len(failed)
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(100, method="squish")],
).dataloaders(path)
dls.show_batch(max_n=6)
learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(1)