Due to the recent proliferation of Internet of Things (IoT) devices, there has been a significant rise in network attacks, often resulting in severe and adverse effects. By exploiting hardware and software vulnerabilities, attackers might utilize these devices through continual internet connectivity to compromise the network infrastructure. Ensuring the security of IoT networks presents a formidable challenge due to the large number of nodes that are limited in resources. As the number of attack cases increases, there is also an importance to deploying an intrusion detection system (IDS) that can monitor and detect various assaults on the devices. Therefore, we introduce an IDS system based on Convolutional Neural Network (CNN) that can detect and classify the types of assaults on IoT devices used in smart environments. In order to reduce the computational overhead and time, we used a feature reduction method and a hyperparameter optimization technique. Our study utilizes two benchmark datasets: the CIC IoT 2023 dataset, which contains real-time attack data, and the UNSW-NB15 dataset, to demonstrate the efficacy of our approach. Important features that are selected using a Genetic algorithm-based feature selection model are converted into color images that are later used as input to different CNN architectures, such as Xception, VGG16 and VGG19 models. The individual classifiers are subsequently combined to create a bagging ensemble model to enhance the system’s performance and dependability. Experimental results suggest that the ensemble model can improve IoT device security by achieving high accuracy in less computing time than single-based classifiers.