A simple demo / tutorial / experiment / portfolio project for me to better understand the concepts of Machine Learning.
Classify Movies by Genre
Use Convolutional Neural Network (CNN) to classify movies posters by genre. It is a multi-label classification problem (movies can belong to multiple genres). Each instance (movie poster) has an independent probability to belong to each label (genre).
The implementation is based on Keras and TensorFlow.
With 14,265 train samples and 2,826 validation samples (movies from 1977 to 2017), 106x161 images and after 50 epochs, the results look like this ([!] indicates the predicted genre is not found in the original dataset):
The Matrix (1999) ['Action: 91%', 'Drama[!]: 25%', 'Adventure[!]: 13%'] The Others (2001) ['Drama[!]: 76%', 'Horror: 65%', 'Action[!]: 41%'] Alien: Resurrection (1997) ['Horror: 67%', 'Action: 64%', 'Drama[!]: 43%'] The Martian (2015) ['Drama: 95%', 'Adventure: 81%', 'Comedy[!]: 23%']
The Truman Show (1998) ['Comedy: 98%', 'Drama: 76%', 'Romance[!]: 7%'] Pretty Woman (1990) ['Romance: 99%', 'Comedy: 99%', 'Drama[!]: 22%'] Whatever Works (2009) ['Drama[!]: 86%', 'Comedy: 78%', 'Romance: 76%'] Bienvenue chez les C.. (2008) ['Comedy: 98%', 'Romance: 98%', 'Drama[!]: 7%']
Paprika (2006) ['Animation: 66%', 'Comedy[!]: 58%', 'Adventure: 31%'] Spirited Away (2001) ['Animation: 83%', 'Drama[!]: 57%', 'Adventure: 42%'] Castle in the Sky (1986) ['Animation: 88%', 'Adventure: 78%', 'Comedy[!]: 30%'] Zootopia (2016) ['Animation: 62%', 'Adventure: 59%', 'Comedy: 49%']
Overall accuracy is 45% (I'm actually not sure it's the most suited metrics for this).
The dataset was found on Kaggle and contains about 27,000 posters.
It is split as followed:
- Training: 5/7
- Validation: 1/7
- Test: 1/7
movies_dataset.py provides functions to access the dataset easily (parse MovieGenres.csv, list movies,
get movie genres, get poster, etc).
- min_year__ and __max_year: Movie release time range (e.g. from 1977 to 2017). Posters design is very dependent on release year, therefore using a larger time range might increase noise.
- genres: Classes. In the current configuration, genres are grouped by 3 (Comedy, Drama, Action), 7 (idem + Animation, Romance, Adventure, Horror) or 14 (idem + Sci-Fi, Crime, Mystery, Thriller, War, Family, Western)
- ratio: Original pictures size is 182x268 (ratio 100). You can use a smaller pictures for quicker (but probably less accurate) model training (30, 40, 50, etc).
- epochs: Number of epochs.
- version: Version of the model. Different versions can have different parameters (e.g. kernel size, etc), so different configurations can be compared easily.
- imagemagick (to resize the original poster image files)
Get posters data
- Use flag
-downloadto download the posters from Amazon (based on the URLs provided in MovieGenre.csv)
- Use flag
-resizeto create smaller posters (30%, 40%, etc)
- Use parameter
-min_year=1980to filter out the oldest movies
python3 get_data.py -download -resize
Train the model
This script builds and trains models. Models are saved to 'saved_models'. One or multiple models (with different parameters) can be produced.
Evaluate the model and test predictions
This script iterates through all the saved models in 'saved_models' and evaluates them on the test data.
Generate Movies Posters with DCGAN
Use Deep Convolutional Generative Adversarial Networks (DCGAN) to generate movie posters:
1. Download the forked DCGAN-tensorflow.
git clone https://github.com/benckx/DCGAN-tensorflow.git
2. Prepare dataset with the parameters you want (git clone this project and download posters first if you didn't):
python3 prepare_dcgan_dataset.py -min_year=1980 -exclude_genres=Animation,Comedy,Family -ratio=60
This will create a folder 'dcgan_movies_posters' with all the posters selected from the parameters values.
3. Move folder 'dcgan_movies_posters' to DCGAN-tensorflow/data/dcgan_movies_posters
4. In DCGAN-tensorflow, run the command with the parameters you need (the parameters I added or removed are documented here):
python3 main.py --dataset dcgan_movies_posters --grid_height=6 --grid_width=10 -sample_rate=2 --train
Run in the Cloud
- AMI: ami-e07e779a. No packages install required.
- Instance type: g2.2xlarge
source activate tensorflow_p36to activate the correct Anaconda environment.
A few things I'm currently working on or thinking about:
- Predict movie release year / rating from the poster
- Improve model versioning to compare different settings (kernel size, loss function, etc.)
- Print neurons state for each genre