def processDrawing(name, export=False): if 'pdf' in name: return processPDF(name) x = Image.open(name).convert('L') (w, h) = x.size wp = int(256.0 * w / min(w, h)) hp = int(256.0 * h / min(w, h)) x = x.resize((wp, hp), Image.BILINEAR) (w, h) = x.size if h > w: center = h / 2 x = x.crop((0, center - 128, 256, center + 128)) elif h < w: center = w / 2 x = x.crop((center - 128, 0, center + 128, 256)) (w, h) = x.size x = np.array(x, np.uint8).reshape((h, w)) / 255.0 x[x > 0.4] = 1 showImage(x) if export: if isinstance(export, str): exportName = export else: exportName = name[:name.index('.')] + '-processed.png' Image.fromarray(x * 255).convert('L').save(exportName) return x
def train(model, inputVar, sess): updateTensor, lossTensor = getUpdateTensor(model, inputVar) sess.run(tf.initialize_all_variables()) start_time = time.time() for iteration in range(numIters): sess.run(updateTensor) if (iteration % showEveryN == 0): img = inputVar.eval() print("Iteration : %s | Loss : %g" % (str(iteration).zfill(4), lossTensor.eval())) utils.showImage(img, imageShape, destDir, str(iteration) + nameAppend) elif (iteration % 10 == 0): print("Iteration : %s | Loss : %g" % (str(iteration).zfill(4), lossTensor.eval())) elapsed = time.time() - start_time print("Experiment Took : %s" % (str(elapsed)))
def induceAbstractions(): results = pickle.load(open(arguments.name, 'rb')) print " [+] Loaded %d synthesis results from %s." % (len(results), arguments.name) def getProgram(index): for r in results: if r.originalDrawing == 'drawings/expert-%d.png' % index: if r.source == None: return None return parseSketchOutput(r.source) return None abstractions = [] for i in range(100): p1 = getProgram(i) if p1 == None: print "No synthesis result for %d" % i continue print "Trying to induce abstractions using:" print p1.pretty() for j in range(i + 1, 100): p2 = getProgram(j) if p2 == None: continue try: a, e = p1.abstract(p2, Environment()) print "SUCCESS:" print p2.pretty() print a.pretty() abstractions.append((i, j, a, e)) except AbstractionFailure: pass abstractionMatrix = [] for i, j, a, e in abstractions: p = a.pretty() if 'for ' in p: print p, "\n" firstProgram = a.substitute( e.firstInstantiation()).convertToSequence() secondProgram = a.substitute( e.secondInstantiation()).convertToSequence() allowUnattached = firstProgram.haveUnattachedLines( ) or secondProgram.haveUnattachedLines() samples = [] desiredNumberOfSamples = 20 samplingAttempts = 0 while len(samples ) < desiredNumberOfSamples and samplingAttempts < 10000: samplingAttempts += 1 concrete = a.substitute( e.randomInstantiation()).convertToSequence() if (not concrete.hasCollisions()\ and (allowUnattached or (not concrete.haveUnattachedLines())))\ or samplingAttempts > 90000: (x0, y0, _, _) = concrete.extent() concrete = concrete.translate(-x0 + 1, -y0 + 1) try: samples.append(concrete.draw()) except ZeroDivisionError: pass samples += [np.zeros( (256, 256)) + 0.5] * (desiredNumberOfSamples - len(samples)) samples = [1 - loadExpert(i), 1 - loadExpert(j)] + samples print firstProgram print firstProgram.haveUnattachedLines() print i print secondProgram print secondProgram.haveUnattachedLines() print j showImage( np.concatenate([firstProgram.draw(), secondProgram.draw()], axis=1)) abstractionMatrix.append(np.concatenate(samples, axis=1)) #.showImage(np.concatenate(abstractionMatrix,axis = 0),) saveMatrixAsImage(255 * np.concatenate(abstractionMatrix, axis=0), 'abstractions.png')
def drawAttentionSequence(background, transformations, l): global FONTSIZE # RGB canvas canvas = np.zeros((256, 256, 3)) * 0.0 colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 1, 1), (1, 0, 1)] # invert the colors: the whole image gets inverted at the end so this makes the colors turn out right colors = [(1 - r, 1 - g, 1 - b) for (r, g, b) in colors] for t, color in zip(transformations, colors): points = [ np.array(applyLinearTransformation(t, p)) * 127 + 128 for p in [(-1.125, -1.125), (-1.125, 1.125), (1.125, 1.125), (1.125, -1.125)] ] for p in points: p[1] = 255 - p[1] for j in range(4): command = Line.absolute(points[j][0] / 16, points[j][1] / 16, points[(j + 1) % 4][0] / 16, points[(j + 1) % 4][1] / 16) output = Sequence([command]).draw() # should be drawn in white for c in range(3): canvas[:, :, c] += output * color[c] # illustrate the order of attention fs = FONTSIZE FONTSIZE = 15 colorX = 1 for j, color in enumerate(colors[:len(transformations)]): output = Sequence([Label(AbsolutePoint(colorX, 15), str(j + 1))]).draw() colorX += 1 for c in range(3): canvas[:, :, c] += output * color[c] FONTSIZE = 8 output = Sequence([Label(AbsolutePoint(8, 1), str(l))]).draw() FONTSIZE = fs canvas[:, :, :] += np.stack([output] * 3, axis=2) canvas[:, :, :] += np.stack([background + Sequence([l]).draw()] * 3, axis=2) canvas[canvas > 1] = 1 canvas = 1 - canvas canvas = (canvas * 255).astype(np.uint8) return canvas showImage(canvas) data = np.flip(data, 0)[:, :, [0, 1, 2]].reshape((256, 256, 3)) showImage(1 - data / 255.0) assert False # add back in the background background = np.stack([background] * 3, axis=2) composite = background + data.astype(np.float32) / 256.0 # add back in the target line l = Sequence([l]).draw() l = np.stack([l] * 3, axis=2) composite += l composite[composite > 1] = 1.0 return 1 - composite
assert False # add back in the background background = np.stack([background] * 3, axis=2) composite = background + data.astype(np.float32) / 256.0 # add back in the target line l = Sequence([l]).draw() l = np.stack([l] * 3, axis=2) composite += l composite[composite > 1] = 1.0 return 1 - composite if __name__ == '__main__': SNAPTOGRID = True s = Sequence.sample(10) print(s) x = render([s.noisyTikZ()], yieldsPixels=True)[0] y = (s.draw()) showImage(np.concatenate([x, y])) # rendering benchmarking startTime = time() N = 100 for _ in range(N): s.draw() print("%f fps" % (N / (time() - startTime)))
def train(model, inputVar, sess): updateTensor, lossTensor = getUpdateTensor(model, inputVar) sess.run(tf.initialize_all_variables()) start_time = time.time() for iteration in range(numIters): sess.run(updateTensor) if (iteration % showEveryN == 0): img = inputVar.eval() print("Iteration : %s | Loss : %g" % (str(iteration).zfill(4), lossTensor.eval())) utils.showImage(img, imageShape, destDir, str(iteration) + nameAppend) elif (iteration % 10 == 0): print("Iteration : %s | Loss : %g" % (str(iteration).zfill(4), lossTensor.eval())) elapsed = time.time() - start_time print("Experiment Took : %s" % (str(elapsed))) with tf.Session() as sess: model = net.Vgg19() inputVar = tf.Variable( tf.random_uniform((1, ) + imageShape, minval=0.25, maxval=0.75)) model.build(inputVar, imageShape) train(model, inputVar, sess) img = inputVar.eval() utils.showImage(img, imageShape, destDir, 'final' + nameAppend)