#!/usr/bin/env python # coding: utf-8 # # Dogs and Cats # In[1]: import conx as cx import os import glob from tqdm import tqdm # First, you'll need to have an account at Kaggle.com. Got over there and log in. # # Next, let's get the data. Go to https://www.kaggle.com/c/dogs-vs-cats/data and download the test1.zip and train.zip files into this folder. # In[2]: cx.download("https://www.kaggle.com/c/3362/download/test1.zip", verbose=0) # In[3]: cx.download("https://www.kaggle.com/c/3362/download/train.zip", verbose=0) # In[4]: get_ipython().run_cell_magic('time', '', 'if not os.path.exists("test_data.h5"):\n def read_data(directory):\n for filename in tqdm(glob.glob(directory)):\n target = [0,0] # we don\'t know what these are\n yield cx.image_to_array(filename, (64,64), raw=True)\n cx.save_data("test_data.h5", read_data("./test1/*.jpg"))\ntest_data = cx.load_data("test_data.h5", return_h5=True)\n') # In[5]: get_ipython().run_cell_magic('time', '', 'if True or not os.path.exists("train_data.h5"):\n targets = []\n def read_data(directory):\n for filename in tqdm(glob.glob(directory)):\n yield [0,1] if "dog" in filename else [1, 0] # cat\n #yield cx.image_to_array(filename, (64,64), raw=True)\n #cx.save_data("train_data.h5", read_data("./train/*.jpg"))\n targets = [x for x in read_data("./train/*.jpg")]\ntrain_data = cx.load_data("train_data.h5", return_h5=True)\n') # In[6]: cx.shape(train_data["data"]), cx.shape(test_data["data"]), cx.shape(targets) # In[7]: net = cx.Network("Dogs and Cats") net.add( cx.ImageLayer("input", (64,64), 3), cx.Convolution2DLayer("conv", 16, (4,4)), cx.FlattenLayer("flatten"), cx.Layer("hidden", 32, activation="relu"), cx.Layer("output", 2, activation="softmax") ) net.connect() net.compile(error="binary_crossentropy", optimizer="rmsprop") # In[8]: net.dataset._targets = [cx.np.array(targets)] net.dataset._inputs = [train_data] net.dataset._labels = [None] * 25000 net.dataset._cache_values() # In[9]: cx.shape(net.dataset.inputs[0]) # In[10]: net.dataset.targets[0] # In[11]: net.dataset.set_labels_from_function(lambda i: "Cat" if net.dataset.targets[i] == [1, 0] else "Dog") # In[12]: net.dashboard() # In[ ]: net.train(100, accuracy=.98) # In[11]: net.evaluate(select=100) # In[ ]: data = net.evaluate_and_label() # In[14]: [x[0] for x in data] # In[12]: import pandas as pd # In[23]: df = pd.DataFrame(data=[net.dataset._labels]) # In[25]: get_ipython().run_line_magic('pinfo', 'net.dataset.set_inputs_from_targets') # In[2]: mnist_hp = cx.Dataset.get("mnist_h5") # In[3]: mnist_hp.save_to_disk("MNIST.conx") # In[2]: ds = cx.Dataset() ds.load_from_disk("MNIST.conx") # In[4]: ds._inputs[0].dtype # In[5]: net # In[ ]: