def get_attributes(self, predictions): predictions = {} for label, predict in self.predict.items(): prediction = predict( np.array(preprocess(image, self.config['image_shape']))[None])[0] predictions[label] = self.labels[label][np.argmax(prediction)] return predictions
def normalize_sample(quantity): # Use the mean and deviance to produce normalized data for an arbitrary image pixel_mean = np.load('pixel_mean.npy') pixel_deviance = np.load('pixel_deviation.npy') for record in list(db.ebay.aggregate([{"$sample": {"size": quantity}}])): # print(record['title']) im = preprocess(Image.open(BytesIO(record['image'])), config['image_shape'], mask=False, letterboxing=False) standardized = (im - pixel_mean) / pixel_deviance print(standardized)
def find_normalizers(): samples = 5000 image_list = [] for listing in db.ebay.aggregate([{"$sample": {"size": samples}}]): image_list.append( np.array( preprocess(Image.open(BytesIO(listing['image'])), config['image_shape'], mask=False, letterboxing=False))) images = np.array(image_list) print(images.shape) print(np.max(images.std(axis=0))) np.save('pixel_mean.npy', images.mean(axis=0)) np.save('pixel_deviation.npy', images.std(axis=0))
def prepare(data, label): # Convert list of dictionaries into two lists of values binaries, tags = [ i for i in zip(*[(stim['image'], stim[label]) for stim in data]) ] # Convert a list of binaries to a 4D stimulus array stim_list = [] for binary in binaries: img = preprocess(Image.open(BytesIO(binary)), config['image_shape']) normalized = (np.array(img) - pixel_mean) / pixel_deviance stim_list.append(np.transpose(normalized, axes=(1, 0, 2))) stim = np.stack(stim_list) # Convert a text label to a onehot encoding exp = np.stack([ np.eye(network.classifications[label])[labels[label].index(tag)] for tag in tags ]) return stim, exp
import numpy as np import sys from Tagger.image_formatter import preprocess # Run once after scraping to remove records with invalid binary data mongo_client = MongoClient(host='localhost', port=27017) # Default port db = mongo_client.deep_fashion count = 0 for document in db.ebay.find({}): try: shape = np.shape( np.array(preprocess(Image.open(BytesIO(document['image']))))) if shape != (224, 224, 3): count += 1 # Show status sys.stdout.write("\r\x1b[KRemoved: " + str(count)) sys.stdout.flush() db.ebay.remove({'image_url': document['image_url']}) except Exception as err: print("\nException: " + document['image_url']) print(err) count += 1 db.ebay.remove({'image_url': document['image_url']})
def checkpoint(check_iteration): print() total_loss = 0 # Take first n records with all attributes query = [{ "$match": {label: { "$exists": True } for label in labels.keys()} }, { "$project": { 'image_url': 1, 'image': 1, **{label: 1 for label in labels.keys()} } }, { "$limit": config['batch_size'] }] # Exhaust the generator into a list samples = list(db.ebay.aggregate(query)) # Print the predicted values for a single sample example = random.choice(samples) # Evaluate losses for all five classifiers for label in labels.keys(): # Reformat sampled data into input/output values stimulus, expected = prepare(samples, label) loss = sess.run(network.loss[label], feed_dict={ network.stimulus: stimulus, network.expected[label]: expected }) img = preprocess(Image.open(BytesIO(example['image'])), config['image_shape'], mask=config['mask'], letterboxing=config['letterbox']) standard = (img - pixel_mean) / pixel_deviance prediction = sess.run(network.predict[label], feed_dict={ network.stimulus: np.transpose(standard, axes=(1, 0, 2))[None] })[0] expectation = np.eye( network.classifications[label])[labels[label].index( example[label])] if config['plot']: history[label].append((int(check_iteration), float(loss))) print("\t" + label + ": " + str(loss) + '\t' + np.array2string(prediction, precision=2) + ' ' + str(expectation)) total_loss += loss print(example['image_url']) # Store the graph if save: save_all.save(sess, save_path + 'model.ckpt') # Plotting if config['plot']: with open(save_path + 'history.json', 'w') as histfile: json.dump(history, histfile) plt.gca().clear() plt.title('Loss') for index, label in enumerate(labels.keys()): plt.plot(*zip(*history[label]), marker='.', color=colors[index], label=label) plt.legend(loc='upper left', prop={'size': 6}) # Draw backend = plt.rcParams['backend'] if backend in matplotlib.rcsetup.interactive_bk: fig_manager = getattr(matplotlib, '_pylab_helpers').Gcf.get_active() if fig_manager is not None: canvas = fig_manager.canvas if canvas.figure.stale: canvas.draw() canvas.start_event_loop(0.00001) # If converged, return false to end the program. Wouldn't this be nice? return total_loss > config['epsilon']