Exemplo n.º 1
0
class OverfeatExtractor:
    def __init__(self, layerNum):
        self.layerNum = layerNum
        self.of = OverfeatTransformer(output_layers=[layerNum])

    def describe(self, data):
        return self.of.transform(data)

    def getFeatureDim(self):
        return SMALL_NETWORK_FILTER_SHAPES[self.layerNum][0]
Exemplo n.º 2
0
class OverfeatExtractor:
    def __init__(self, layerNum):
        # store the layer number and initialize the Overfeat transformer
        self.layerNum = layerNum
        self.of = OverfeatTransformer(output_layers=[layerNum])

    def describe(self, data):
        # apply the Overfeat transform to the images
        return self.of.transform(data)

    def getFeatureDim(self):
        # return the feature dimensionality from the supplied layer
        return SMALL_NETWORK_FILTER_SHAPES[self, layerNum][0]
Exemplo n.º 3
0
class ConvQAgent(PacmanQAgent):
  def __init__(self, epsilon=0.05,gamma=0.8,alpha=0.2, numTraining=0, **args):
        """
        These default parameters can be changed from the pacman.py command line.
        For example, to change the exploration rate, try:
            python pacman.py -p PacmanQLearningAgent -a epsilon=0.1

        alpha    - learning rate
        epsilon  - exploration rate
        gamma    - discount factor
        numTraining - number of training episodes, i.e. no learning after these many episodes
        """
        args['epsilon'] = epsilon
        args['gamma'] = gamma
        args['alpha'] = alpha
        args['numTraining'] = numTraining
        self.index = 0  # This is always Pacman
        QLearningAgent.__init__(self, **args)

        self.tf = OverfeatTransformer(output_layers = [-1], force_reshape = False)
        self.weights = util.Counter()

  def featureExtractor(self):

    screen = np.array(ImageGrab.grab(bbox = (50, 120, 1250, 650)))
    small_screen = block_reduce(screen, (530/224, 1200/224, 1), np.max)
    features = np.array(self.tf.transform(small_screen[:,:,0:3])).flatten()

    feat = dict(zip(range(2000), features))

    return  feat
  def getQValue(self, state, action):
    features = self.featureExtractor()
    total = 0
    for feat in features:
      total += feat * self.weights[feat]
    return total

  def update(self, state, action, nextState, reward):
      candidateQ = reward + self.discount * \
          self.computeValueFromQValues(nextState)
      currentQ = self.getQValue(state, action)
      features = self.featureExtractor()
      difference = candidateQ - currentQ
      for feat in features:
        self.weights[feat] += self.alpha * difference * feat
Exemplo n.º 4
0
class Classifier(object):
    def __init__(self, configPath):
        self.config = json.load(open(configPath))
        self.model = cPickle.load(open(self.config["classifier_path"]))
        self.le = cPickle.load(open(self.config["label_encoder_path"]))
        self.overfeat = OverfeatTransformer(output_layers=self.config["layer_num"])

    def _preprocess(self, image):
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = cv2.resize(image, tuple(self.config["image_size"]))
        return image

    def _extract_overfeat_features(self, image):
        image = self._preprocess(image)
        features = self.overfeat.transform([image])
        return features

    def predict(self, imagePath):
        image = cv2.imread(imagePath)
        features = self._extract_overfeat_features(image)[0]
        prediction = self.model.predict([features])[0]
        return self.le.inverse_transform(prediction)
Exemplo n.º 5
0
import cv2
import decimal
import json
import ijson.backends.yajl2_cffi as ijson
from sklearn_theano.feature_extraction import OverfeatTransformer

tr = OverfeatTransformer(output_layers=[8])


class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            return float(o)
        return super(DecimalEncoder, self).default(o)


with open('../workspace/ds.json') as inh:
    with open('../workspace/ds_deep.json', 'w') as outh:
        ds = ijson.items(inh, 'item')
        outh.write('[')

        for i, item in enumerate(ds):
            print 'running', i + 1
            if i > 0:
                outh.write(',')
            img = cv2.imread('set1/' + item['file'])
            img = cv2.resize(img, (231, 231))
            item['deep'] = tr.transform(img)[0].tolist()
            json.dump(item, outh, cls=DecimalEncoder)

        outh.write(']')
Exemplo n.º 6
0
    cmap = {}

    ct = 0
    for k, v in catlists.iteritems():
        print v['name']
        cnt = 0
        cmap[ct] = {'id': k, 'name': v['name']}
        for fn in v['images']:
            img = cv2.imread(fn)
            if img is None:
                continue

            crop = simpleProcessImage(img)
            csz = crop.shape
            if csz[0] == 231 and csz[1] == 231:
                res = tranformer.transform(crop)
            else:
                continue
            #print res.shape
            #print res[res>0.0]

            if features is None:
                features = res
            else:
                features = np.vstack((features, res))
            ctype.append(ct)
            #cv2.imshow('X', crop)
            #cv2.waitKey()
            cnt += 1
            if cnt > 500:
                break
import cv2
import decimal
import json
import ijson.backends.yajl2_cffi as ijson
from sklearn_theano.feature_extraction import OverfeatTransformer

tr = OverfeatTransformer(output_layers=[8])

class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            return float(o)
        return super(DecimalEncoder, self).default(o)

with open('../workspace/ds.json') as inh:
    with open('../workspace/ds_deep.json', 'w') as outh:
        ds = ijson.items(inh, 'item')
        outh.write('[')

        for i, item in enumerate(ds):
            print 'running', i+1
            if i > 0:
                outh.write(',')
            img = cv2.imread('set1/' + item['file'])
            img = cv2.resize(img, (231, 231))
            item['deep'] = tr.transform(img)[0].tolist()
            json.dump(item, outh, cls=DecimalEncoder)

        outh.write(']')
f, axarr = plt.subplots(2, 3)
X = load_sample_image("sloth.jpg")
axarr[0, 0].imshow(X)
axarr[0, 0].axis('off')

# Show a single box
axarr[0, 1].imshow(X)
axarr[0, 1].axis('off')
r = Rectangle((0, 0), 231, 231, fc='yellow', ec='black', alpha=.8)
axarr[0, 1].add_patch(r)

# Show all the boxes being processed
axarr[0, 2].imshow(X)
axarr[0, 2].axis('off')
clf = OverfeatTransformer(force_reshape=False)
X_tf = clf.transform(X)
x_points = np.linspace(0, X.shape[1] - 231, X_tf[0].shape[3])
y_points = np.linspace(0, X.shape[0] - 231, X_tf[0].shape[2])
xx, yy = np.meshgrid(x_points, y_points)
for x, y in zip(xx.flat, yy.flat):
    axarr[0, 2].add_patch(Rectangle((x, y), 231, 231, fc='yellow', ec='black',
                          alpha=.4))

# Get all points with sloth in the top 5 labels
sloth_label = "three-toed sloth, ai, Bradypus tridactylus"
clf = OverfeatLocalizer(match_strings=[sloth_label])
sloth_points = clf.predict(X)[0]
axarr[1, 0].imshow(X)
axarr[1, 0].axis('off')
axarr[1, 0].autoscale(enable=False)
axarr[1, 0].scatter(sloth_points[:, 0], sloth_points[:, 1], color='orange',
f, axarr = plt.subplots(2, 3)
X = load_sample_image("sloth.jpg")
axarr[0, 0].imshow(X)
axarr[0, 0].axis('off')

# Show a single box
axarr[0, 1].imshow(X)
axarr[0, 1].axis('off')
r = Rectangle((0, 0), 231, 231, fc='yellow', ec='black', alpha=.8)
axarr[0, 1].add_patch(r)

# Show all the boxes being processed
axarr[0, 2].imshow(X)
axarr[0, 2].axis('off')
clf = OverfeatTransformer(force_reshape=False)
X_tf = clf.transform(X)
x_points = np.linspace(0, X.shape[1] - 231, X_tf[0].shape[3])
y_points = np.linspace(0, X.shape[0] - 231, X_tf[0].shape[2])
xx, yy = np.meshgrid(x_points, y_points)
for x, y in zip(xx.flat, yy.flat):
    axarr[0, 2].add_patch(
        Rectangle((x, y), 231, 231, fc='yellow', ec='black', alpha=.4))

# Get all points with sloth in the top 5 labels
sloth_label = "three-toed sloth, ai, Bradypus tridactylus"
clf = OverfeatLocalizer(match_strings=[sloth_label])
sloth_points = clf.predict(X)[0]
axarr[1, 0].imshow(X)
axarr[1, 0].axis('off')
axarr[1, 0].autoscale(enable=False)
axarr[1, 0].scatter(sloth_points[:, 0],
from sklearn_theano.datasets import fetch_asirra
from sklearn_theano.feature_extraction import OverfeatTransformer
import matplotlib.pyplot as plt
import time

asirra = fetch_asirra()
X = asirra.images.astype("float32")
X = X[0:5]
y = asirra.target
all_times = []
for i in range(0, 15):
    tf = OverfeatTransformer(output_layers=[i])
    t0 = time.time()
    X_tf = tf.transform(X)
    print("Shape of layer %i output" % i)
    print(X_tf.shape)
    t_o = time.time() - t0
    all_times.append(t_o)
    print("Time for layer %i" % i, t_o)
    print()
plt.plot(all_times, marker="o")
plt.title("Runtime for input to layer X")
plt.xlabel("Layer number")
plt.ylabel("Time (seconds)")
plt.show()
Exemplo n.º 11
0
    cmap = {}

    ct = 0
    for k,v in catlists.iteritems():
        print v['name']
        cnt=0
        cmap[ct] = {'id':k, 'name':v['name']}
        for fn in v['images']:
            img = cv2.imread(fn)
            if img is None:
                continue

            crop = simpleProcessImage(img)
            csz = crop.shape
            if csz[0]==231 and csz[1]==231:
                res = tranformer.transform(crop)
            else:
                continue
            #print res.shape
            #print res[res>0.0]

            if features is None:
                features = res
            else:
                features = np.vstack((features, res))
            ctype.append(ct)
            #cv2.imshow('X', crop)
            #cv2.waitKey()
            cnt+=1
            if cnt>500:
                break
Exemplo n.º 12
0
print("[INFO] encoding labels...")
le = LabelEncoder()
le.fit([p.split("/")[-2] for p in imagePaths])

print("[INFO] initializing network...")
overfeat = OverfeatTransformer(output_layers=config["layer_num"])

print("[INFO] extracting features...")

for start in xrange(0, len(imagePaths), batch_size):
    end = start + batch_size

    #read and resize the images
    images = [cv2.imread(impath) for impath in imagePaths[start:end]]
    images = [cv2.cvtColor(image, cv2.COLOR_BGR2RGB) for image in images]
    images = np.array(
        [cv2.resize(image, tuple(config["image_size"])) for image in images],
        dtype="float")
    #dump the image ID and features to the hdf5 database
    imageIDDB[start:end] = [
        ":".join(impath.split("/")[-2:]) for impath in imagePaths[start:end]
    ]
    features = overfeat.transform(np.array(images))
    featuresDB[start:end] = overfeat.transform(np.array(images))
    print("[INFO] processed {} images".format(end))

db.close()
f = open(config["label_encoder_path"], "w")
cPickle.dump(le, f)
f.close()
from sklearn_theano.datasets import fetch_asirra
from sklearn_theano.feature_extraction import OverfeatTransformer
import matplotlib.pyplot as plt
import time

asirra = fetch_asirra()
X = asirra.images.astype('float32')
X = X[0:5]
y = asirra.target
all_times = []
for i in range(0, 15):
    tf = OverfeatTransformer(output_layers=[i])
    t0 = time.time()
    X_tf = tf.transform(X)
    print("Shape of layer %i output" % i)
    print(X_tf.shape)
    t_o = time.time() - t0
    all_times.append(t_o)
    print("Time for layer %i" % i, t_o)
    print()
plt.plot(all_times, marker='o')
plt.title("Runtime for input to layer X")
plt.xlabel("Layer number")
plt.ylabel("Time (seconds)")
plt.show()
f, axarr = plt.subplots(2, 3)
X = load_sample_image("sloth.jpg")
axarr[0, 0].imshow(X)
axarr[0, 0].axis('off')

# Show a single box
axarr[0, 1].imshow(X)
axarr[0, 1].axis('off')
r = Rectangle((0, 0), 231, 231, fc='yellow', ec='black', alpha=.8)
axarr[0, 1].add_patch(r)

# Show all the boxes being processed
axarr[0, 2].imshow(X)
axarr[0, 2].axis('off')
clf = OverfeatTransformer(force_reshape=False)
X_tf = clf.transform(X[None].astype('float32'))
x_points = np.linspace(0, X.shape[1] - 231, X_tf[0].shape[3])
y_points = np.linspace(0, X.shape[0] - 231, X_tf[0].shape[2])
xx, yy = np.meshgrid(x_points, y_points)
for x, y in zip(xx.flat, yy.flat):
    axarr[0, 2].add_patch(Rectangle((x, y), 231, 231, fc='yellow', ec='black',
                          alpha=.4))

# Get all points with sloth in the top 5 labels
sloth_label = [label for label in get_all_overfeat_labels()
               if 'three-toed sloth' in label][0]
clf = OverfeatLocalizer(match_strings=[sloth_label])
sloth_points = clf.predict(X.astype('float32'))[0]
axarr[1, 0].imshow(X)
axarr[1, 0].axis('off')
axarr[1, 0].autoscale(enable=False)
f, axarr = plt.subplots(2, 3)
X = load_sample_image("sloth.jpg")
axarr[0, 0].imshow(X)
axarr[0, 0].axis("off")

# Show a single box
axarr[0, 1].imshow(X)
axarr[0, 1].axis("off")
r = Rectangle((0, 0), 231, 231, fc="yellow", ec="black", alpha=0.8)
axarr[0, 1].add_patch(r)

# Show all the boxes being processed
axarr[0, 2].imshow(X)
axarr[0, 2].axis("off")
clf = OverfeatTransformer(force_reshape=False)
X_tf = clf.transform(X[None].astype("float32"))
x_points = np.linspace(0, X.shape[1] - 231, X_tf[0].shape[3])
y_points = np.linspace(0, X.shape[0] - 231, X_tf[0].shape[2])
xx, yy = np.meshgrid(x_points, y_points)
for x, y in zip(xx.flat, yy.flat):
    axarr[0, 2].add_patch(Rectangle((x, y), 231, 231, fc="yellow", ec="black", alpha=0.4))

# Get all points with sloth in the top 5 labels
sloth_label = "three-toed sloth, ai, Bradypus tridactylus"
clf = OverfeatLocalizer(match_strings=[sloth_label])
sloth_points = clf.predict(X.astype("float32"))[0]
axarr[1, 0].imshow(X)
axarr[1, 0].axis("off")
axarr[1, 0].autoscale(enable=False)
axarr[1, 0].scatter(sloth_points[:, 0], sloth_points[:, 1], color="orange", s=50)