Skip to content

Instantly share code, notes, and snippets.

@trishume
Created May 14, 2018 02:45
Show Gist options
  • Save trishume/99a161c5c3653c08edfbf9e1cd6d27a5 to your computer and use it in GitHub Desktop.
Save trishume/99a161c5c3653c08edfbf9e1cd6d27a5 to your computer and use it in GitHub Desktop.
DEF CON Quals 2018 Flagsifier Notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# DC Quals Flagsifier\n",
"\n",
"Summary of the solution for posterity: load up the EMNIST dataset and write functions to compose strings into images like the examples and run them through the net, find the category with the highest activation for `OOO` and then blank space, then go character by character choosing the character that gives the highest activation out of 30 random samples of that character from the dataset. Repeat this process starting from the previous result and tuning it up character by character, so any cross-character dependencies in the dense layer can be satisfied. Get a garbled mess with one intelligible word, everyone contributes to figuring out a meaningful english interpretation out of all the mistakes.\n",
"\n",
"Key tactics: I think stripping the softmax from the end so that there's more resolution in the feedback for hill climbing, and doing a second tuning pass were both necessary but they're non-obvious. I'm also guessing that it's hard to get deep dream to get you something readable, because otherwise someone would have done it, although it may be that it would only work with stripping the softmax and nobody tried that yet."
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [],
"source": [
"from keras.models import load_model\n",
"from keras.preprocessing import image\n",
"import keras\n",
"import numpy as np\n",
"from scipy import io as spio\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 177,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv2d_1 (Conv2D) (None, 26, 1062, 32) 320 \n",
"_________________________________________________________________\n",
"activation_1 (Activation) (None, 26, 1062, 32) 0 \n",
"_________________________________________________________________\n",
"average_pooling2d_1 (Average (None, 13, 531, 32) 0 \n",
"_________________________________________________________________\n",
"dropout_1 (Dropout) (None, 13, 531, 32) 0 \n",
"_________________________________________________________________\n",
"conv2d_2 (Conv2D) (None, 11, 529, 32) 9248 \n",
"_________________________________________________________________\n",
"activation_2 (Activation) (None, 11, 529, 32) 0 \n",
"_________________________________________________________________\n",
"average_pooling2d_2 (Average (None, 5, 264, 32) 0 \n",
"_________________________________________________________________\n",
"dropout_2 (Dropout) (None, 5, 264, 32) 0 \n",
"_________________________________________________________________\n",
"conv2d_3 (Conv2D) (None, 3, 262, 64) 18496 \n",
"_________________________________________________________________\n",
"activation_3 (Activation) (None, 3, 262, 64) 0 \n",
"_________________________________________________________________\n",
"average_pooling2d_3 (Average (None, 1, 131, 64) 0 \n",
"_________________________________________________________________\n",
"dropout_3 (Dropout) (None, 1, 131, 64) 0 \n",
"_________________________________________________________________\n",
"flatten_1 (Flatten) (None, 8384) 0 \n",
"_________________________________________________________________\n",
"dense_1 (Dense) (None, 512) 4293120 \n",
"_________________________________________________________________\n",
"activation_4 (Activation) (None, 512) 0 \n",
"_________________________________________________________________\n",
"dropout_4 (Dropout) (None, 512) 0 \n",
"_________________________________________________________________\n",
"dense_2 (Dense) (None, 40) 20520 \n",
"_________________________________________________________________\n",
"activation_5 (Activation) (None, 40) 0 \n",
"=================================================================\n",
"Total params: 4,341,704\n",
"Trainable params: 4,341,704\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n",
"<function linear at 0x1141b7268>\n",
"<keras.models.Sequential object at 0x124009da0>\n"
]
}
],
"source": [
"# model_lin.h5 is just model.h5 with the JSON model description in the binary\n",
"# for the last layer patched from \"softmax\", to \"linear\" ,\n",
"# because of the way compilation works, patching was easiest\n",
"model = load_model('model_lin.h5')\n",
"model.summary()\n",
"print(model.get_layer(name='activation_5').activation)\n",
"print(model)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(None, 28, 1064, 1)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.input_shape"
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [],
"source": [
"# model.get_layer(name='activation_5').activation = keras.activations.linear"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# EMNIST Loading"
]
},
{
"cell_type": "code",
"execution_count": 246,
"metadata": {},
"outputs": [],
"source": [
"emnist = spio.loadmat(\"emnist/emnist-bymerge.mat\")\n"
]
},
{
"cell_type": "code",
"execution_count": 247,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(697932, 784)"
]
},
"execution_count": 247,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# load training dataset\n",
"x_train = emnist[\"dataset\"][0][0][0][0][0][0]\n",
"x_train = x_train.astype(np.float32)\n",
"\n",
"# load training labels\n",
"y_train = emnist[\"dataset\"][0][0][0][0][0][1]\n",
"# load test dataset\n",
"x_test = emnist[\"dataset\"][0][0][1][0][0][0]\n",
"x_test = x_test.astype(np.float32)\n",
"\n",
"# load test labels\n",
"y_test = emnist[\"dataset\"][0][0][1][0][0][1]\n",
"\n",
"x_train.shape"
]
},
{
"cell_type": "code",
"execution_count": 248,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(697932, 28, 28, 1)"
]
},
"execution_count": 248,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# reshape using matlab order\n",
"x_train = x_train.reshape(x_train.shape[0], 28, 28, 1, order=\"A\")\n",
"x_test = x_test.reshape(x_test.shape[0], 28, 28, 1, order=\"A\")\n",
"x_train.shape"
]
},
{
"cell_type": "code",
"execution_count": 249,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(697932, 1)"
]
},
"execution_count": 249,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_train.shape"
]
},
{
"cell_type": "code",
"execution_count": 250,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(281109, 28, 28, 1)"
]
},
"execution_count": 250,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x_uppercase = np.concatenate((x_train,x_test))\n",
"y_uppercase = np.concatenate((y_train,y_test))\n",
"\n",
"is_uppercase = (y_uppercase >= 10) & (y_uppercase <= 35)\n",
"x_uppercase = x_uppercase[is_uppercase[:,0]]\n",
"y_uppercase = y_uppercase[is_uppercase]\n",
"\n",
"x_uppercase.shape"
]
},
{
"cell_type": "code",
"execution_count": 262,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADrFJREFUeJzt3X+MVPW5x/HPIwL+oFER3BCrUhttbNRYQ/QaiLFREE0TwD+0mpg1VhdD1YvemKteoybmJloLpvEHSlNSuLlKTWwjQby2oPdComlErSDoVqxLABe4iBERtS48/WMPzYqc75mdOTNnluf9SjY7c545Mw8TPnvOnO+Z8zV3F4B4Dqu6AQDVIPxAUIQfCIrwA0ERfiAowg8ERfiBoAg/EBThB4I6vJUvZmacTgg0mbtbLY9raMtvZlPNrNvMNpjZnY08F4DWsnrP7TezYZL+KmmypM2SXpd0tbuvT6zDlh9oslZs+c+TtMHd/+buf5e0WNK0Bp4PQAs1Ev4TJW0acH9ztuwbzKzLzFab2eoGXgtAyZp+wM/d50uaL7HbD7STRrb8WySdNOD+d7NlAIaARsL/uqTTzOx7ZjZC0k8lLSmnLQDNVvduv7v3mdnNkl6SNEzSAndfV1pnAJqq7qG+ul6Mz/xA07XkJB8AQxfhB4Ii/EBQhB8IivADQRF+ICjCDwRF+IGgCD8QFOEHgiL8QFCEHwiK8ANBEX4gKMIPBEX4gaAIPxAU4QeCIvxAUIQfCIrwA0ERfiAowg8ERfiBoAg/EBThB4Ii/EBQhB8IivADQdU9RbckmVmPpM8k7ZXU5+4TymgKQPM1FP7Mj919RwnPA6CF2O0Hgmo0/C7pj2b2hpl1ldEQgNZodLd/krtvMbMTJP3JzN5z95UDH5D9Ue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_type": "display_data"
}
],
"source": [
"# 10=A 35=Z\n",
"# samplei = np.where(y_uppercase == 35)[0][0]\n",
"samplei = 76230\n",
"img = x_uppercase[samplei]\n",
"\n",
"# visualize image\n",
"def showlet(img):\n",
" plt.imshow(img.reshape(img.shape[0],img.shape[1]), cmap='gray')\n",
"\n",
"showlet(img)"
]
},
{
"cell_type": "code",
"execution_count": 259,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x124e48048>"
]
},
"execution_count": 259,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADrFJREFUeJzt3X+MVPW5x/HPIwL+oFER3BCrUhttbNRYQ/QaiLFREE0TwD+0mpg1VhdD1YvemKteoybmJloLpvEHSlNSuLlKTWwjQby2oPdComlErSDoVqxLABe4iBERtS48/WMPzYqc75mdOTNnluf9SjY7c545Mw8TPnvOnO+Z8zV3F4B4Dqu6AQDVIPxAUIQfCIrwA0ERfiAowg8ERfiBoAg/EBThB4I6vJUvZmacTgg0mbtbLY9raMtvZlPNrNvMNpjZnY08F4DWsnrP7TezYZL+KmmypM2SXpd0tbuvT6zDlh9oslZs+c+TtMHd/+buf5e0WNK0Bp4PQAs1Ev4TJW0acH9ztuwbzKzLzFab2eoGXgtAyZp+wM/d50uaL7HbD7STRrb8WySdNOD+d7NlAIaARsL/uqTTzOx7ZjZC0k8lLSmnLQDNVvduv7v3mdnNkl6SNEzSAndfV1pnAJqq7qG+ul6Mz/xA07XkJB8AQxfhB4Ii/EBQhB8IivADQRF+ICjCDwRF+IGgCD8QFOEHgiL8QFCEHwiK8ANBEX4gKMIPBEX4gaAIPxA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Rf9DGVdTbJPXv0q+R9Jfs5/Kq37tEX5W8b5zhBwTFAT8gKMIPBEX4gaAIPxAU4QeCIvxAUIQfCIrwA0H9AzQM4auxfNuBAAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"sample_img = image.load_img('images/sample_0.png', target_size=(28, 1064), grayscale=True)\n",
"sample_img = image.img_to_array(sample_img)\n",
"i = 28*22\n",
"img = sample_img[0:28,i+0:i+28]\n",
"plt.imshow(img.reshape(img.shape[0],img.shape[1]), cmap='gray')"
]
},
{
"cell_type": "code",
"execution_count": 260,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"76230\n"
]
}
],
"source": [
"haystack = x_uppercase\n",
"for i in range(haystack.shape[0]):\n",
" img2 = haystack[i]\n",
" if np.array_equal(img, img2):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Figuring out categories\n",
"\n",
"There's a 40 class categorical output, I have no idea what the classes are."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"index = {}\n",
"for i in range(10,36):\n",
" index[i] = np.where(y_uppercase == i)[0][0]\n",
"index"
]
},
{
"cell_type": "code",
"execution_count": 280,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([[-10.2729225 , -6.246442 , 21.704496 , 9.9872465 ,\n",
" -1.4535668 , -11.526137 , -11.214999 , -8.216931 ,\n",
" -3.161952 , -6.9192376 , -10.277658 , -9.55106 ,\n",
" -1.7620567 , 0.70177466, -12.314412 , -8.018769 ,\n",
" -1.6116011 , -5.5214314 , -9.404995 , -7.675523 ,\n",
" -0.7550617 , -10.754059 , -1.7025601 , -0.04662082,\n",
" -12.097055 , -4.800998 , -5.375687 , -13.148593 ,\n",
" -6.128149 , -14.216559 , -1.9360402 , -12.2938595 ,\n",
" -0.99819237, -6.7235403 , -9.595497 , -11.14351 ,\n",
" -6.8396316 , -13.304192 , -4.475975 , -6.852819 ]],\n",
" dtype=float32), array([2]), 21.704496)"
]
},
"execution_count": 280,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAAsCAYAAAB1yUN9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAEnpJREFUeJztnX9snEV6x7+zP7xe51pCuvEl5A4Ws8QVsZKcsVoQlUgaOK5HHNhiRUVWa1oiKlQosVyVSyNniaIiFS+9XNDpFJTiSqvqDuIQaBAqlHCRUiEna4iJSZx17D2KuWD7XIWwWpt9vTvf/vH+YH+867Udxybr+UiP7Hdm3nln5pl55pl5931fQRIKhUKhuP5xLHYBFAqFQjE/KIOuUCgUZYIy6AqFQlEmKIOuUCgUZYIy6AqFQlEmKIOuUCgUZcJVGXQhxI+EEDEhxKAQ4ifzVSiFQqFQzB4x19+hCyGcAAYA3A/gcwBRAI+SPD9/xVMoFArFTLkaD/2PAAySjJPUAPwKwEPzUyyFQqFQzJarMehrAAxnHX9uhCkUCoViEXBd6wsIIZ4A8IRxeOe1vp7iGzweD9LpNDKZzDW/VkVFBTRNu+bXUSwtXC4X0un0Yhfj28A4yZWlEl2Nh/5bAN/POv6eEZYDyZdJNpBsyA6vrq5Gd3c3nE6nbeafffYZRkZG0NjYWLIgQgjbcI/Hg2g0Cikl0uk0NE1Df3//tHndcMMNcDhm1yxOpxOjo6OQUoIkpJQYGBiYVR6zZd26dQgEAmhtbcXmzZvh9/tz4kOhEFKpFKampmzP9/l8GB0dRSwWQzQaRVdXF6qrq0tet5jRTiQS0563YcMGBAKBovFutxuBQABut3va9t++fTtcrtn7IX6/H5qmIZVKob+/H6FQyJL8PiaEwMDAAC5dulQy3/7+/qLlicViM+5LmqbB5/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KBSKMkEZdIVCoSgTlEFXKBSKMkEZdIVCoSgTlEFXKBSKMkEZdIVCoSgTlEFXKBSKMuH/Ab8blGNrABQ+AAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"space = np.zeros((28,28,1))\n",
"\n",
"def pick_letter(cls):\n",
" if cls == -23: # space\n",
" return -1\n",
" poss = np.where(y_uppercase == cls)[0]\n",
" return np.random.choice(poss)\n",
"\n",
"def compose_image(s, seed=42):\n",
" np.random.seed(seed)\n",
" classes = [ord(c)-ord('A')+10 for c in s]\n",
" indices = [pick_letter(c) for c in classes]\n",
" letters = [(x_uppercase[i] if i > 0 else space) for i in indices]\n",
" img = np.hstack(np.array(letters))\n",
" return img\n",
"\n",
"# s1 = 'MOLDERINGIINTELSDEDUCINGCOYNESSDEFIECT'\n",
"# s1 = ' ' * 38\n",
"s1 = 'OOOSNMFMWTASNJLOIWTCKLIUCWCCXSGTCUIETJ'\n",
"s2 = 'O' * len(s1)\n",
"i = 3\n",
"\n",
"# img = compose_image('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')\n",
"img = compose_image(s2[:i] + s1[i:])\n",
"showlet(img)\n",
"\n",
"probs = model.predict(np.expand_dims(img,axis=0) / 255.0)\n",
"classes = probs.argmax(axis=1)\n",
"probs2 = probs\n",
"(probs,classes,probs[0,classes[0]])"
]
},
{
"cell_type": "code",
"execution_count": 300,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Starting\n",
"(30, 'OOOSOMEAUTHENTICINTELLIGENCEISRTQUIVCT', 327, 'R', 56.426052)\n",
"(31, 'OOOSOMEAUTHENTICINTELLIGENCEISRSQUIVCT', 348, 'S', 56.597042)\n",
"(32, 'OOOSOMEAUTHENTICINTELLIGENCEISRSDUIVCT', 350, 'D', 56.515118)\n",
"(33, 'OOOSOMEAUTHENTICINTELLIGENCEISRSDUIVCT', 325, 'U', 56.4094)\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-300-8d5977e9b505>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Starting\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 62\u001b[0m \u001b[0mseeds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m936\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m936\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m936\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m940\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m937\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m951\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m952\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m941\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m960\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m937\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m947\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m949\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m944\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m937\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m948\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m957\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m948\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m937\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m937\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m951\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m943\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m950\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m948\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m947\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m957\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m937\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m957\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m957\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m943\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m940\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m943\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m937\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m934\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m960\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m948\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m942\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m956\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m936\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 63\u001b[0;31m \u001b[0msearch_all\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'OOOSOMEAUTHENTICINTELLIGENCEISRTQUIVCT'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseeds\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtries\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m40\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseed_base\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m323\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mstart\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 64\u001b[0m \u001b[0;31m# OOOSNMFMWTASNJLOIWTCKLIUCWCCXSGTCUIETJ\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[0;31m# OONJOMCMNTMSNTIUINTTILIGCWCCISKCONIRFT\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-300-8d5977e9b505>\u001b[0m in \u001b[0;36msearch_all\u001b[0;34m(base, base_seeds, cls, tries, seed_base, start)\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0mseeds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbase_seeds\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstart\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m38\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 52\u001b[0;31m \u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mseed\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfind_best\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseeds\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtries\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseed_base\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 53\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0mseeds\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mseed\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-300-8d5977e9b505>\u001b[0m in \u001b[0;36mfind_best\u001b[0;34m(i, base_s, base_seeds, cls, tries, seed_base)\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0mseeds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbase_seeds\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[0mseeds\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mseed\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 41\u001b[0;31m \u001b[0mprob\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtest_with\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m''\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseeds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mcls\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 42\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mprob\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0mmax_prob\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[0mbest_c\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-300-8d5977e9b505>\u001b[0m in \u001b[0;36mtest_with\u001b[0;34m(s, seeds)\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;31m# print(s)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0mfull_seeds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mseeds\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m38\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mseeds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 22\u001b[0;31m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcompose_image2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mljust\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m38\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfull_seeds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 23\u001b[0m \u001b[0;31m# showlet(img)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0minp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexpand_dims\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-300-8d5977e9b505>\u001b[0m in \u001b[0;36mcompose_image2\u001b[0;34m(s, seeds)\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;31m# print(s)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mclasses\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mord\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mord\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'A'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m10\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mc\u001b[0m \u001b[0;32min\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mindices\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mpick_letter2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ms\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mclasses\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseeds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 15\u001b[0m \u001b[0mletters\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_uppercase\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mspace\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mindices\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mletters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-300-8d5977e9b505>\u001b[0m in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;31m# print(s)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mclasses\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mord\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mord\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'A'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m10\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mc\u001b[0m \u001b[0;32min\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mindices\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mpick_letter2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0ms\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mclasses\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseeds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 15\u001b[0m \u001b[0mletters\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_uppercase\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mspace\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mindices\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mletters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-300-8d5977e9b505>\u001b[0m in \u001b[0;36mpick_letter2\u001b[0;34m(cls, s)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;31m# print(cls)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mposs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwhere\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my_uppercase\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mseed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchoice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mposs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"space = np.zeros((28,28,1))\n",
"\n",
"def pick_letter2(cls,s):\n",
" if cls == -23: # space\n",
" return -1\n",
"# print(cls)\n",
" poss = np.where(y_uppercase == cls)[0]\n",
" np.random.seed(s)\n",
" return np.random.choice(poss)\n",
"\n",
"def compose_image2(s, seeds):\n",
"# print(s)\n",
" classes = [ord(c)-ord('A')+10 for c in s]\n",
" indices = [pick_letter2(c,s) for c,s in zip(classes, seeds)]\n",
" letters = [(x_uppercase[i] if i > 0 else space) for i in indices]\n",
" img = np.hstack(np.array(letters))\n",
" return img\n",
"\n",
"def test_with(s, seeds):\n",
"# print(s)\n",
" full_seeds = seeds + [0]*(38-len(seeds))\n",
" img = compose_image2(s.ljust(38), full_seeds)\n",
"# showlet(img)\n",
" inp = np.expand_dims(img,axis=0)\n",
" inp /= 255\n",
" probs = model.predict(inp)\n",
" return probs[0]\n",
"\n",
"def find_best(i, base_s, base_seeds, cls, tries=10, seed_base=0):\n",
" best_c = ' '\n",
" best_seed = 0\n",
" max_prob = -10000000.0\n",
" for ci in range(ord('A'),ord('Z')):\n",
" for sample in range(tries):\n",
" c = chr(ci)\n",
" s = base_s[:]\n",
" s[i] = c\n",
" seed = sample+seed_base\n",
" seeds = base_seeds[:]\n",
" seeds[i] = seed\n",
" prob = test_with(''.join(s), seeds)[cls]\n",
" if prob > max_prob:\n",
" best_c = c\n",
" best_seed = seed\n",
" max_prob = prob\n",
" return (best_c, best_seed, max_prob)\n",
"\n",
"# class 2 was found to be the best class by looking at activations for\n",
"# strings starting with OOO\n",
"def search_all(base='', base_seeds=[], cls=2, tries=10, seed_base=0, start=0):\n",
" s = list(base)\n",
" seeds = base_seeds\n",
" for i in range(start,38):\n",
" c,seed,p = find_best(i, s, seeds, cls, tries, seed_base)\n",
" s[i] = c\n",
" seeds[i] = seed\n",
" print((i,''.join(s),seed,c,p))\n",
" return ''.join(s), seeds\n",
"\n",
"# test_with('OO')[2]\n",
"# find_best('OOOTN',2)\n",
"# find_best('OOO',2)\n",
"print(\"Starting\")\n",
"seeds = [936,936,936,940,937,951,952,941,960,937,947,949,944,937,948,957,948,937,937,951,943,950,948,947,957,937,957,957,943,940,943,937,934,960,948,942,956,936]\n",
"search_all('OOOSOMEAUTHENTICINTELLIGENCEISRTQUIVCT', seeds, tries=40, seed_base=323,start=30)\n",
"# OOOSNMFMWTASNJLOIWTCKLIUCWCCXSGTCUIETJ\n",
"# OONJOMCMNTMSNTIUINTTILIGCWCCISKCONIRFT\n",
"# OOOIOMCMUTMENTICIWTCILIGCWCCLIRCOUICFO\n",
"# OOOSOMGAUTHKNTICIWTTILIGCWCCISRTQUIVCT"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment