[PYTHON] [Part 1] Use Deep Learning to forecast the weather from weather images

change history

** 2016/8/31 Corrected because the AUC calculation only saw "fine" **

Overview

Speaking of CNN (Convolutional Neural Network) in Deep Learning, image processing is the main thing, but I have not done any analysis on image processing, so this time I would like to verify ** whether weather forecast can be done from weather images ** think.

Preparation

Obtaining weather images

The meteorological image was downloaded from Kochi University site. Get the data from January 2015 to July 2016. I used the image like this. In addition, since there is data every hour, I got the data at 17:00.

fe.16082717.jpg

Source: Provided by Kochi University, University of Tokyo, Japan Meteorological Agency

Get past weather

Past weather can be obtained from the [Page] of the Japan Meteorological Agency (http://www.data.jma.go.jp/risk/obsdl/index.php). This is also the daily weather data from January 2015 to July 2016. The place is Tokyo and I used the daytime weather.

Problem setting

The problem is "predicting the next day's fine weather and rain from the weather image near Japan at 17:00 the day before." The weather of the Japan Meteorological Agency includes "rain after fine weather" and "cloudy", but if rain is included, it is treated as "rain", otherwise it is treated as "fine", and it is a binary classification problem.

Preprocessing

Tomorrow's weather will be affected not only by the movement of clouds near Tokyo but also by westerlies, so we need a wider range of meteorological images, but we don't need the weather far away from Japan. So, I will crop the image to make the data near Japan.

import numpy as np
from PIL import Image
import datetime as dt

w = 640
h = 480
"""Setting to cut out only around Japan"""
sw = 320
sh = 65
ew = 540
eh = 320
"""Image compression"""
is_comp = False

def get_mat(dates=[]):
    """

    :param dates:
    :return:
    """
    l = len(dates)
    if not is_comp:
        wr = ew - sw
        hr = eh - sh
    else:
        wr = 50
        hr = 50

    mat = np.zeros((l,3,wr,hr),dtype=np.float32)
    file_base = base_file_dir + "fe.%s" + base_hour + ".jpg "

    j = 0
    err_dates = []
    for ddd in dates:
        dd = dt.datetime.strptime(ddd,"%Y/%m/%d")
        dd_str = dd.strftime("%y%m%d")
        try:
            im = Image.open(file_base % (dd_str))
            im = im.crop((sw,sh,ew,eh))
            im = im.resize((wr, hr))
            mat0 = np.array(im)
            for i in range(0,3):
                mat[j,i,:,:] = mat0[:,:,i].T
            j += 1
        except:
            err_dates.append(ddd)
            print dd_str + " --> Error!!"
    return mat[0:j],err_dates

It takes a list of dates, returns the images as a numpy matrix, and also returns the date of the error. The image object im contains an image, but if you convert it to a matrix with numpy, it will be in the order of (width, height, channel), so be careful that it is transposed. Here, the image (480x640) is trimmed to the extent that the Japanese archipelago is included. It looks like the following.

test02.jpg

Since the data after making a matrix contains data from 0 to 255 in each cell, divide it by 255 and convert it to data from 0 to 1.

Due to the learning time of CNN after that, I thought about compressing the image, but this time I stopped.

CNN model

The model of the convolutional neural network uses the one used before. Konohen or Konohen or Konohen Please refer to .com / wbh / items / da881fac695f17042b19).

The parameter settings are as follows.

params = {"clm_dim":clm_dim,"in_channels":3,"out_channels":3,"row_dim":row_dim,"filt_clm":3,"filt_row":3,"pool_clm":3,"pool_row":3,"batchsize":200,"hidden_dim":500,"n_classes":2}

clm_dim and row_dim correspond to the width and height of each image. Also, since the number of channels is RGB, it is set to 3. Also use max pooling. If you increase the filter size or pooling size, it will take more time to calculate. (hooked on)

Even with this setting, it takes a lot of time on my macbook pro. So, this time I set epoch = 50.

Training data and test data

Since it takes a lot of calculation time, the training data is for one year from 2015/1/1 to 2015/12/31, and the test data is from 2016/1/1 to 2016/7/31. Even this takes time. Image processing is scary ...

Model training and test accuracy

The learning process and test accuracy are as follows. train_test.png

Also, the learning accuracy is likely to increase, but the test accuracy is not catching up.

test results

In AUC ~~ 0.78 ~~ ** 0.70 **, various indicators are as follows.

Precision Recall F-Score
rain 0.54 0.67 0.6
Fine 0.83 0.74 0.78
average 0.74 0.72 0.72

I think it's a pretty good feeling for what I did. However, of the forecasts of rain, the actual amount of rain was as low as 54%.

Let's plot the actual rain ratio and the fine ratio by arranging them in descending order with the probability of rain.

test_ruiseki.png

It seems that it is not random but predictive. That's right, because the actual rain ratio in the test data is 30% and the sunny ratio is 60%.

Handling of cloudy weather

It seems that one of the reasons why the accuracy does not improve is the treatment of "cloudiness". There are cases of "sunny and then cloudy" and "cloudy temporary rain", and the actual cloudy ratio in the data exceeds 70%. (Too cloudy) This time, the former is treated as fine and the latter as rain. Therefore, it seems that you do not know which is the delicate situation. This is a challenge. However, in this result, it seems that the obvious rain is hit with a good degree of accuracy.

It is a difficult problem because cloudiness becomes dominant in the ternary classification.

The weather forecaster is amazing.

from now on

I'm wondering if this is the algorithm, but for the data, for example, it is possible to take the difference from the previous day's image or remove the pixels of the base map. I think I'll do it little by little.

Recommended Posts

[Part 4] Use Deep Learning to forecast the weather from weather images
[Part 1] Use Deep Learning to forecast the weather from weather images
[Part 3] Use Deep Learning to forecast the weather from weather images
[Part 2] Use Deep Learning to forecast the weather from weather images
Deep Learning from the mathematical basics Part 2 (during attendance)
I tried to implement Perceptron Part 1 [Deep Learning from scratch]
Reinforcement learning to learn from zero to deep
Image alignment: from SIFT to deep learning
"Deep Learning from scratch" Self-study memo (Part 12) Deep learning
Get data from Poloniex, a cryptocurrency exchange, via API and use deep learning to forecast prices for the next day.
About the order of learning programming languages (from beginner to intermediate) Part 2
Deep Learning beginners tried weather forecasting from meteorological satellite images using Keras
Tweet the weather forecast with a bot Part 2
Deep Learning from scratch ① Chapter 6 "Techniques related to learning"
POST images from ESP32-CAM (MicroPython) to the server
Stock Price Forecast Using Deep Learning (TensorFlow) -Part 2-
Deep Learning from scratch
Create a dataset of images to use for learning
I wanted to use the Python library from MATLAB
Othello ~ From the tic-tac-toe of "Implementation Deep Learning" (4) [End]
[Deep Learning from scratch] I tried to explain Dropout
Deep Learning from scratch 1-3 chapters
Paper: Machine learning paper that reproduces images in the brain, (Deep image reconstruction from human brain activity)
How to use the generator
[Deep learning] Investigating how to use each function of the convolutional neural network [DW day 3]
[Deep Learning from scratch] I tried to explain the gradient confirmation in an easy-to-understand manner.
[Python] I asked LINE BOT to answer the weather forecast.
How to increase the number of machine learning dataset images
(Deep learning) Images were collected from the Flickr API and discriminated by transfer learning with VGG16.
Introduction to Deep Learning ~ Learning Rules ~
I captured the Touhou Project with Deep Learning ... I wanted to.
Deep Reinforcement Learning 1 Introduction to Reinforcement Learning
"Deep Learning from scratch" Self-study memo (Part 8) I drew the graph in Chapter 6 with matplotlib
How to use the decorator
Introduction to Deep Learning ~ Backpropagation ~
[Deep Learning from scratch] About the layers required to implement backpropagation processing in a neural network
Chapter 1 Introduction to Python Cut out only the good points of deep learning made from scratch
[Machine learning] Understand from mathematics why the correlation coefficient ranges from -1 to 1.
Lua version Deep Learning from scratch Part 6 [Neural network inference processing]
How to use machine learning for work? 01_ Understand the purpose of machine learning
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
Deep Learning / Deep Learning from Zero 2 Chapter 4 Memo
How to use the zip function
How to use the optparse module
Deep Learning / Deep Learning from Zero Chapter 3 Memo
How to use SWIG from waf
Deep Learning / Deep Learning from Zero 2 Chapter 5 Memo
Introduction to Deep Learning ~ Function Approximation ~
Try deep learning with TensorFlow Part 2
Deep learning from scratch (cost calculation)
Deep learning to start without GPU
Introduction to Deep Learning ~ Coding Preparation ~
Post images from Python to Tumblr
Use the Flickr API from Python
Deep Learning / Deep Learning from Zero 2 Chapter 7 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 8 Memo
Deep Learning / Deep Learning from Zero Chapter 5 Memo
Deep Learning / Deep Learning from Zero Chapter 4 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 3 Memo
Deep Learning memos made from scratch