Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Training GAN on Azure Machine Learning to Produce Art

Training GAN on Azure Machine Learning to Produce Art

This talk is both introduction to Generative Adversarial Networks, and to Azure Machine Learning. I use GAN as an example to show how convenient it is to perform computationally intensive neural network training on Azure Machine Learning, and discuss what advantages it gives to the Data Scientist.

Dmitri Soshnikov

June 02, 2020
Tweet

More Decks by Dmitri Soshnikov

Other Decks in Programming

Transcript

  1. Training GAN on Azure Machine Learning to Produce Art Dmitry

    Soshnikov, Cloud Developer Advocate, Microsoft http://soshnikov.com – @shwars
  2. AI / Machine Learning on Azure Domain specific pretrained models

    To reduce time to market Azure Databricks Machine Learning VMs Popular frameworks To build advanced deep learning solutions TensorFlow Pytorch Onnx Azure Machine Learning Language Speech … Search Vision Productive services To empower data science and development teams Powerful infrastructure To accelerate deep learning Scikit-Learn PyCharm Jupyter Familiar Data Science tools To simplify model development Visual Studio Code Command line CPU GPU FPGA From the Intelligent Cloud to the Intelligent Edge
  3. Where does Azure ML fit? Data Science VM Azure ML

    Cognitive Services Infrastructure services Platform Services Pre-trained Models For beginners Advanced Data Science Experimentation
  4. How to Start with Azure ML: Read my blog series:

    • The best way to start with Azure ML using VS Code • Using Azure ML for Hyperparameter Optimization • Training GAN to Produce Art • Training BERT Question Answering with DeepPavlov ❶ ❷ Try it out: http://github.com/CloudAdvocacy/AzureMLStarter
  5. Azure ML Workspace: A container for Everything Azure ML Workspace

    encapsulates it all: 1. Storage 2. Datasets 3. Compute 4. Notebooks 5. Experiment Results 6. Models 7. Deployments ❶ az extension add -n azure-cli-ml az group create -n ml -l westus2 az ml workspace create -w AzML -g ml az ml folder attach -w AzML -g ml Create Workspace using Azure CLI: az ml computetarget create amlcompute -n cpu --min-nodes 0 --max-nodes 2 -s STANDARD_D3_V2 Create Cluster using Azure CLI: MS Docs: HERE
  6. Submit and Track Experiments Experiment is represented by a Python

    Script + Environment that run on Compute (Local Compute, Azure ML Cluster or Databricks) 1. Auto-package code 2. Keep track of results 3. Store models 4. Queue runs 5. Programmatically spawn many runs with different parameters ❷ az ml run submit-script -c sklearn –e MyExp train.py Submit Experiment using CLI: Log Metrics in the script: from azureml.core.run import Run run = Run.get_submitted_run() run.log('accuracy', acc)
  7. Toy Problem: MNIST Digit Recognition http://yann.lecun.com/exdb/mnist/ mnist=fetch_openml('mnist_784’) X = mnist[‘data’]

    y = mnist[‘target’] fig,ax=plt.subplots(1,15) for i in range(15): z=X_train[i].reshape(28,28) ax[i].imshow(z) plt.show()
  8. My Computer Data Store Azure ML Workspace Compute Target Docker

    Image How Azure ML Experimentation Works Experiment
  9. Run Notebooks and Create Datasets When you do a lot

    of training, it makes sense to store data inside the workspace. To run Python code inside the workspace – use Notebooks! You need to create separate compute (not cluster) to do that! ❸
  10. Submitting Experiments / Training Script Use args to pass different

    parameters, including data path parser = argparse.ArgumentParser(description='MNIST Train') parser.add_argument(‘--data_folder', type=str, dest='data_folder', help='data folder mount point') parser.add_argument('--epochs', type=int, default=3) parser.add_argument('--batch_size', type=int, default=128) parser.add_argument('--hidden', type=int, default=100) parser.add_argument('--dropout', type=float) Store model into outputs directory os.makedirs('outputs',exist_ok=True) model.save('outputs/mnist_model.hdf5') Load data as files fn = os.path.join(args.data_folder, 'mnist_data/mnist.pkl') with open(fn,'rb') as f: X,y = pickle.load(f) run = Run.get_context() run.log('Test Loss', score[0]) run.log('Accuracy', score[1]) Log Result ❹
  11. Tools for Simplified ML: AutoML, Designer Automatic ML ❻ Designer

    Run the experiment to automatically try different models and select the one that performs best Can do some feature optimization (data balancing, irrelevant feature elimination) Similar to Azure ML Studio Classic Perform ML experiments without coding, by composing pre-build blocks Defines pipelines in a graphical way
  12. GAN Library: keragan http://github.com/shwars/keragan Generator Discriminator Random Noise (dim=100) Conv

    Matrix Conv Matrix Reshape DeConv DeConv Conv Matrix Conv Matrix Feature Vector Classifier
  13. GAN Library: keragan http://github.com/shwars/keragan Generator Discriminator discriminator = Sequential() for

    x in [16,32,64]: # number of filters on next layer discriminator.add(Conv2D(x, (3,3), strides=1, padding="same")) discriminator.add(AveragePooling2D()) discriminator.addBatchNormalization(momentum=0.8)) discriminator.add(LeakyReLU(alpha=0.2)) discriminator.add(Dropout(0.3)) discriminator.add(Flatten()) discriminator.add(Dense(1, activation='sigmoid')) generator = Sequential() generator.add(Dense(8 * 8 * 2 * size, activation="relu", input_dim=latent_dim)) generator.add(Reshape((8, 8, 2 * size))) for x in [64;32;16]: generator.add(UpSampling2D()) generator.add(Conv2D(x, kernel_size=(3,3),strides=1, padding="same")) generator.add(BatchNormalization()) generator.add(Activation("relu")) generator.add(Conv2D(3, kernel_size=3, padding="same")) generator.add(Activation("tanh"))
  14. Model Training discriminator.trainable = False noise = np.random.normal(0, 1, (batch_size,

    latent_dim)) gen_imgs = generator.predict(noise) imgs = get_batch(batch_size) d_loss_r = discriminator.train_on_batch(imgs, ones) d_loss_f = discriminator.train_on_batch(gen_imgs, zeros) d_loss = np.add(d_loss_r , d_loss_f)*0.5 g_loss = combined.train_on_batch(noise, ones) res = generator.predict(np.random.normal(3,latent_dim)) fig,ax = plt.subplots(1,len(res)) for i,v in enumerate(res): ax[i].imshow(v[0]) run.log_image("Sample",plot=plt) # Generate Noise Vector & Images # Train Discriminator # Train Generator (by training combined model) # Log Sample Images through Azure ML Code Sample on GitHub Code Sample on GitHub
  15. Getting and Using the Model fnames = list(filter(lambda x :

    x.startswith('outputs/models/gen_’), run.get_file_names())) no = max(map(lambda x: int(x[19:x.find('.')]), fnames)) fname = 'outputs/models/gen_{}.h5'.format(no) run.download_file(fname) # Get the Latest Model File model = keras.models.load_model(fname) latent_dim=model.layers[0].input.shape[1].value vec = np.random.normal(0,1,(10,latent_dim)) res = model.predict(vec)) res = (res+1.0)/2 # Predict 10 images Code Sample on GitHub
  16. Conclusions Azure ML enhances your ML experience by: • Grouping

    everything together in workspace • Journaling all experiment results automatically • Helping with hyperparameter optimization and scalable compute • Supporting distributed training ❶ ❷ You should try it out: • http://github.com/CloudAdvocacy/ AzureMLStarter • http://aka.ms/azmlstarter - Blog Post
  17. Further Reading  How to train your own neural network

    to generate paintings http://aka.ms/azml_gan  Can AI be creative http://aka.ms/creative_ai  Creating interactive exhibit based on cognitive portraits http://aka.ms/cognitive_portrait_exhibit  Training COVID ODQA on Azure ML: http://aka.ms/deeppavlov