def __init__(self, labels=CRAFTED_LABELS): """ Detects fish from the OverFeat network Localizer Reference: P Sermanet, D. Eigen, X. Zhang, M. Mathieu, R. Fergus, Y. LeCun. OverFeat: Integrated Recognition, Localization, and Detection using Convolutional Networks International Conference on Learning Representations (ICLR 2014), April 2014. """ self.localizer = OverfeatLocalizer(match_strings=labels, top_n=2)
class FishLocalizer(object): def __init__(self, labels=CRAFTED_LABELS): """ Detects fish from the OverFeat network Localizer Reference: P Sermanet, D. Eigen, X. Zhang, M. Mathieu, R. Fergus, Y. LeCun. OverFeat: Integrated Recognition, Localization, and Detection using Convolutional Networks International Conference on Learning Representations (ICLR 2014), April 2014. """ self.localizer = OverfeatLocalizer(match_strings=labels, top_n=2) def find(self, image, around=15): """ Find potential fish locations in an image @params image: the image to extract fish from around: the pixes around the detected fish position to extract from @returns list of images containing extracted fish """ points = self.localizer.predict(image) extracted_fish = [] for p, point in enumerate(points): gmm = GMM() try: # use a GMM to find the fish positions gmm.fit(point) ulp_x, ulp_y, w, h = convert_gmm_to_box(gmm) ulp_x = int(ulp_x) - around if ulp_x > around else 0 ulp_y = int(ulp_y) - around if ulp_y > around else 0 w, h = int(w), int(h) if w == 0 or h == 0: continue # extract the fish extracted_fish.append(image[ulp_y:ulp_y + h + around, ulp_x:ulp_x + w + around, :]) except Exception as e: # the gmm could not be fit, possibly due to not enough points # @TODO: check if the points are the reason for skipping here print str(e) #end try #endfor return extracted_fish
def convert_gmm_to_box(gmm, color, alpha): midpoint = gmm.means_ std = 3 * np.sqrt(clf.covars_) width = std[:, 0] height = std[:, 1] upper_left_point = (midpoint[:, 0] - width // 2, midpoint[:, 1] - height // 2) return Rectangle(upper_left_point, width, height, ec=color, fc=color, alpha=alpha) X = load_sample_image("cat_and_dog.jpg") dog_label = 'dog.n.01' cat_label = 'cat.n.01' clf = OverfeatLocalizer(top_n=1, match_strings=[dog_label, cat_label]) points = clf.predict(X) dog_points = points[0] cat_points = points[1] plt.imshow(X) ax = plt.gca() ax.autoscale(enable=False) clf = GMM() clf.fit(dog_points) dog_box = convert_gmm_to_box(clf, "darkred", .6) clf.fit(cat_points) cat_box = convert_gmm_to_box(clf, "steelblue", .6) ax.add_patch(dog_box) ax.add_patch(cat_box) ax.get_xaxis().set_ticks([])
# Show all the boxes being processed axarr[1, 0].imshow(X / 255.) axarr[1, 0].axis('off') # Hard code box size to speed up processing x_points = np.linspace(0, X.shape[1] - 231, 13) y_points = np.linspace(0, X.shape[0] - 231, 10) xx, yy = np.meshgrid(x_points, y_points) for x, y in zip(xx.flat, yy.flat): axarr[1, 0].add_patch(Rectangle((x, y), 231, 231, fc='yellow', ec='black', alpha=.4)) print("Starting localization") # 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, 1].imshow(X / 255.) axarr[1, 1].axis('off') axarr[1, 1].autoscale(enable=False) axarr[1, 1].scatter(sloth_points[:, 0], sloth_points[:, 1], color='orange', s=50) print("Localization complete!") def convert_gmm_to_box(gmm, color, alpha): midpoint = gmm.means_ std = 3 * np.sqrt(clf.covars_) width = std[:, 0] height = std[:, 1] upper_left_point = (midpoint[:, 0] - width // 2,
# 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', s=50) # Final localization box sloth_box = convert_points_to_box(sloth_points, 'orange', .6) axarr[1, 1].imshow(X) axarr[1, 1].autoscale(enable=False) axarr[1, 1].add_patch(sloth_box) axarr[1, 1].axis('off')
def convert_gmm_to_box(gmm, color, alpha): midpoint = gmm.means_ std = 3 * np.sqrt(clf.covars_) width = std[:, 0] height = std[:, 1] upper_left_point = (midpoint[:, 0] - width // 2, midpoint[:, 1] - height // 2) return Rectangle(upper_left_point, width, height, ec=color, fc=color, alpha=alpha) X = load_sample_image("cat_and_dog.jpg") dog_label = 'dog.n.01' cat_label = 'cat.n.01' clf = OverfeatLocalizer(top_n=1, match_strings=[dog_label, cat_label]) points = clf.predict(X.astype('float32')) dog_points = points[0] cat_points = points[1] plt.imshow(X) ax = plt.gca() ax.autoscale(enable=False) clf = GMM() clf.fit(dog_points) dog_box = convert_gmm_to_box(clf, "darkred", .6) clf.fit(cat_points) cat_box = convert_gmm_to_box(clf, "steelblue", .6) ax.add_patch(dog_box) ax.add_patch(cat_box) ax.get_xaxis().set_ticks([])
print(__doc__) import matplotlib.pyplot as plt from matplotlib.patches import Rectangle from sklearn_theano.datasets import load_sample_image from sklearn_theano.feature_extraction import OverfeatLocalizer from sklearn_theano.feature_extraction import get_all_overfeat_labels def convert_points_to_box(points, color, alpha): upper_left_point = (points[:, 0].min(), points[:, 1].min()) width = points[:, 0].max() - points[:, 0].min() height = points[:, 1].max() - points[:, 1].min() return Rectangle(upper_left_point, width, height, ec=color, fc=color, alpha=alpha) X = load_sample_image("sloth.jpg") 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] sloth_box = convert_points_to_box(sloth_points, 'orange', .4) plt.imshow(X) ax = plt.gca() ax.autoscale(enable=False) ax.add_patch(sloth_box) ax.get_xaxis().set_ticks([]) ax.get_yaxis().set_ticks([]) plt.show()
print(__doc__) import matplotlib.pyplot as plt from matplotlib.patches import Rectangle from sklearn_theano.datasets import load_sample_image from sklearn_theano.feature_extraction import OverfeatLocalizer from sklearn_theano.feature_extraction import get_all_overfeat_labels def convert_points_to_box(points, color, alpha): upper_left_point = (points[:, 0].min(), points[:, 1].min()) width = points[:, 0].max() - points[:, 0].min() height = points[:, 1].max() - points[:, 1].min() return Rectangle(upper_left_point, width, height, ec=color, fc=color, alpha=alpha) X = load_sample_image("sloth.jpg") 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)[0] sloth_box = convert_points_to_box(sloth_points, 'orange', .4) plt.imshow(X) ax = plt.gca() ax.autoscale(enable=False) ax.add_patch(sloth_box) ax.get_xaxis().set_ticks([]) ax.get_yaxis().set_ticks([]) plt.show()